Page 1 of 1

preg_replace help

Posted: Sun Dec 27, 2009 9:26 am
by 8ennett
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,

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;  
}
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,

Code: Select all

echo '<tr><td>'.BBCode($row['descr']).'</td><td>'.date('g:i jS M Y', $row['time']).'</td></tr>';
Any help is greatly appreciated!

Re: preg_replace help

Posted: Sun Dec 27, 2009 3:02 pm
by requinix

Code: Select all

$var = array(1, 2, 3, username("$1"));
$var[3] will be whatever the username() function returned using "$1" as the ID.

Code: Select all

$simple_replace = array("", "", "", username("$1"));
Again, $simple_replace[3] will be whatever the username() function returned using "$1" as the ID.

What you want is to make preg_replace evaluate code.

Code: Select all

// replace
'/\[username\](.*?)\[\/username\]/e'
// with
'username("$1")'

Re: preg_replace help

Posted: Sun Dec 27, 2009 4:48 pm
by 8ennett
that's really strange, that was the second thing i tried and took it straight out of the php manual but wouldnt work, has this time though, thanks a lot for the help!