preg_replace()在MySQL表中查找匹配项


preg_replace() find matches in MySQL Table

    function convText($v){
    $str = htmlentities($v);
    preg_match_all('('['[(.+?)']'])', $str, $matches);
    foreach($matches['0'] as $match){
        $substring = substr($match, 1);
        $getData = bandToLink($substring);
        $str = preg_replace('/$match/', $getData , $str);
    }
    return $str;
    }
function bandToLink($v){
    $findBand = mysql_query("select * from news where news_title='". $v ."'") or die(mysql_error());
    if(mysql_num_rows($findBand)==0){
        return '<strong style="color:red">$v</strong>';
    } else {
        $getFind = mysql_fetch_assoc($findBand);
        return '<a href="/band/'. $getFind['seo_link'] .'.html">'. $getFind['news_title'] .'</a>';
    }
}
$text = "Lorem ipsum dolor sit amet, [[APPLE]] and Lorem [[CHERRY]] ipsum dolor";
echo convText($text);
如果在数据库中发现APPLE,如何将[[APPLE]]文本替换为<a href="#">APPLE</a> ?我使用这种模式的ASP功能相同的作品,但它不工作在php.

您忘记在正则表达式中包含分隔符。应该是:

preg_match_all('/('['[(.+?)']'])/', $str, $matches);