2014-11-24 148 views
0

我正在寫一個ittle腳本,用對應html標籤替換bbcodes。 bbcodes和html標籤都存儲在數據庫中。這是我的[B] [/ B],但它並不適用用html標籤替換bbcode

function bbcode($matches) 
{ 
$conn = mysqli_connect('127.0.0.1','root','','esame') or die("Connection failed: " . $conn->connect_error); 
foreach ($matches as $match) 
    { 
    $query = mysqli_query($conn,"SELECT html FROM `bbcode` WHERE bbcode='{$match}' "); 
     while ($html = mysqli_fetch_array($query,MYSQLI_ASSOC)) 
     { 
     return 'Html: '.$html['html']; 
     } 
    $conn->close(); 
    } 
} 

$regex = '/\[b\](.+)\[\/b\]/is'; 
echo '<br>'.preg_replace_callback($regex,"bbcode",$text).'<br>'; 
+0

它返回某種錯誤的實際代碼? – 2014-11-24 10:33:52

+0

不,它只是返回輸入字符串,例如[b] foo [/ b] – Antonio102 2014-11-24 10:36:09

+0

preg_replace_callback的文檔頁面有一個帶有bbcode函數的用戶註釋。也許你可以看看那個'靈感'。 ;) – GolezTrol 2014-11-24 10:45:38

回答

0
$str = "[list=1]\n[*]Камиль [/*]\n[*]Хисматуллин [/*]\n[*]живет в Урюпинске [/*]\n[/list]"; 

$advanced_bbcode = array(
    '/(?si)\\[list=\\d+\\](.*?)\\[\\/list\\]/', 
    '/(?si)\\[\\*\\](.*?)\\[\\/\\*\\]/' 
); 

$advanced_html = array(
    '<ol>$1</ol>', 
    '<li>$1</li>' 
); 

$text = preg_replace($advanced_bbcode, $advanced_html, $str); 
echo $text;