PHP preg_replace weirdness
Posted: May 25th, 2009, 12:14
Any PHP bods got a spare sec? A little weirdness with a regular expression thing is confusing my little brain
Baically, i have a need to search a string for a reference, eg: [brandid:10]
then take that brand ID, 10, into a database and get its proper name.
So, blah blah blah [brandid:10] blah
becomes
blah blah blah <a href="/visit?brandten">brandten</a> blah
So far, so good with a little preg_replace:
$content = preg_replace('/\[brandid:(.*)]/', test1('$1'), $content);
Where the function is:
so it is taking the supplied id, giving it to the function and getting the right result.
So, now i need to get it from a database
If i call this new function separately, it works fine. If i call it in the preg_replace as above, it fails miserably, returning blank and gives me no indication of what has gone wrong.
Anyone got any thoughts as to why? Cheers muchly!!
// note: phpbb seems to remove various things from the code, i have escaped things and such
Baically, i have a need to search a string for a reference, eg: [brandid:10]
then take that brand ID, 10, into a database and get its proper name.
So, blah blah blah [brandid:10] blah
becomes
blah blah blah <a href="/visit?brandten">brandten</a> blah
So far, so good with a little preg_replace:
$content = preg_replace('/\[brandid:(.*)]/', test1('$1'), $content);
Where the function is:
Code: Select all
function test1($text) {
return "<a href="/visit/$text">$text</a>";
}
So, now i need to get it from a database
Code: Select all
function test3($id) {
$sql = mysql_query ("SELECT name FROM brands WHERE id=$id");
// return value
$row = mysql_fetch_array($sql);
return "<a href="/visit/".$row[name].">".$row[name]."</a>";
}
Anyone got any thoughts as to why? Cheers muchly!!
// note: phpbb seems to remove various things from the code, i have escaped things and such