php - preg_replace() find matches in MySQL Table -
function convtext($v){ $str = htmlentities($v); preg_match_all('(\[\[(.+?)\]\])', $str, $matches); foreach($matches['0'] $match){ $substring = substr($match, 1); $getdata = bandtolink($substring); $str = preg_replace('/$match/', $getdata , $str); } return $str; } function bandtolink($v){ $findband = mysql_query("select * news 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]] , lorem [[cherry]] ipsum dolor"; echo convtext($text);
how can replace [[apple]] texts <a href="#">apple</a>
, if apple found in database? have using pattern on asp function same works, doesn't work on php.
you forgot include delimiters in regular expression. should be:
preg_match_all('/(\[\[(.+?)\]\])/', $str, $matches);
Comments
Post a Comment