【发布时间】:2013-03-24 23:05:49
【问题描述】:
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。
【问题讨论】:
标签: php mysql preg-replace preg-match