2012-10-24 48 views
1

请我制作WAP论坛,我希望管理员能够从数据库中名为mycodes与列添加bbcodes:ID,名称,代码,HTMLPHP预浸替换bbcodes

Row1 
Name: bold 
Code: \[b\](.*?)\[/b] 
Html: <b>$1</b > 

Row2 
Name: undaline 
Code: \[u\](.*?)\[/u] 
Html: <u>$1</u > 

当我使用预浸取代它只有当我有一行,如果我有多个它不会工作,它只会解析粗体,但不强调?

function myparse($text){ 
    $q = mysql_query("SELECT * FROM mycodes"); 
    while($row = mysql_fetch_array($q)) { 
    $code=$row['code']; 
    $html=$row['html'] 
    $Result=preg_replace('#'.$code.'#is', $html, $text); 
    return $result; 
    } 
} 

myparse("hey am [b]bold[/b] but he is [u]undalined[/u]"); 
+0

'$ row'从哪里来?如果你想应用多个表达式,我希望看到某种类型的循环。 –

回答

0

我看不到任何东西在你的myparse函数循环遍历你的代码行。因此,根据您目前的代码,你需要一个循环多次调用了preg_replace:

function myparse($text){ 
    // Loop through rows (this might be a database or whatever stores your rows. 
    // Since your code doesn't tell us I'll assume it's an array for now 
    foreach ($rows as $row) { 
     $code=$row['code']; 
     $html=$row['html']; 
     $Result=preg_replace('#'.$code.'#is', $html, $text); 
    } 
} 
+0

我已编辑它现在检查 – Lordcash

+0

请回复我,我编辑它 – Lordcash

0

你有几个错误,在你的代码。正确的功能应该是这样的:

function myparse($text){ 
    $q = mysql_query("SELECT * FROM mycodes"); 
    while($row = mysql_fetch_array($q)) { 
     $code=$row['code']; 
     $html=$row['html'] 
     $text=preg_replace('#'.$code.'#is', $html, $text); 
    } 
    return $text; 
} 

在你的代码中 - 实际上只使用mycodes中的第一行。