preg_replace help
Posted: Sun Dec 27, 2009 9:26 am
I've got a problem returning something with preg_replace.
The below code is my sites BBCode function, it's the last preg_replace entry in the array,
I'm trying to have it return the username from the DB with the id value $1 in between the [username][/username] tags by using the first function above called userName() but just can't get it to work.
The line calling the function is,
Any help is greatly appreciated!
The below code is my sites BBCode function, it's the last preg_replace entry in the array,
Code: Select all
// Returns a users name based on id
function userName($id){
$query = mysql_query("SELECT username FROM userlist WHERE id=".$id);
$query = mysql_fetch_array($query);
return $query['username'];
}
// BBCode
function BBCode($str) {
$str = htmlentities($str);
$simple_search = array(
'/\[b\](.*?)\[\/b\]/is', // Bold
'/\[url\](.*?)\[\/url\]/is', // Plain Url
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is', // Text alignment
'/\[img\](.*?)\[\/img\]/is', // Unsized image
'/\[size\=(.*?)\](.*?)\[\/size\]/is', // Change size
'/\[colour\=(.*?)\](.*?)\[\/colour\]/is', // Change colour (UK)
'/\[youtube\](.*?)\[\/youtube\]/is', // Unsized Youtube video
'/\n/is', // New line (automatic)
'/\[username\](.*?)\[\/username\]/' // Displays the user id's current name
);
$simple_replace = array(
'<strong>$1</strong>',
'<a href="$1" rel="nofollow" title="$1" target="_blank">$2</a>',
'<div style="text-align: $1;">$2</div>',
'<img src="$1" alt="" />',
'<span style="font-size: $1px;">$2</span>',
'<span style="color: #$1;">$2</span>',
'<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/\\1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/\\1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>',
'<br />',
userName("$1")
);
$str = preg_replace ($simple_search, $simple_replace, $str);
return $str;
}The line calling the function is,
Code: Select all
echo '<tr><td>'.BBCode($row['descr']).'</td><td>'.date('g:i jS M Y', $row['time']).'</td></tr>';