0
    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]]文本替换为?我在 ASP 函数上使用此模式进行相同的工作,但它不适用于 php。<a href="#">APPLE</a>

4

1 回答 1

1

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

preg_match_all('/(\[\[(.+?)\]\])/', $str, $matches);
于 2013-03-24T23:09:37.780 回答