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);

How can I replace [[APPLE]] texts to <a href="#">APPLE</a>, If APPLE found in database? I have using this pattern on ASP function for same works, but its doesn't work on php.

4

1 に答える 1

1

You forgot to include delimiters in your regular expression. It should be:

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