Assume in the below code that there is a match for #bob and #test, what would be the preg_replace expression so that #bob and #to are urls (ie http://test.com/read.php?id=bob)? Note there is no hashtag in the id=bob part.
$text = "my name is #bob and I like #to #test things.";
preg_match_all("/(#\w+)/", $text, $matches);
foreach($matches['0'] as $match){
$substring = substr($match, 1);
$titlerows = mysql_num_rows(mysql_query("SELECT title FROM title WHERE title='$substring'"));
if($titlerows == 1) {
$text = preg_replace('/$match/', replace?? , $text);
}
}
echo $text;
Like FeRtoll’s answer, but this takes advantage of the fact that we already did the
substr()earlier:For the more complex replacement in the comment, do:
I like interpolation rather than concatenation.