Hey, I am trying to write a sort of replace function where [tt]{user:1}[/tt] is converted into a set of information from a database using preg_replace. So I have created the following preg_replace:
[php]$patterns = array();
$patterns[1] = ‘/{user:([0-9]+)}/’;
$replacements = array();
$replacements[1] = UserCredit(’$1’);
ksort($patterns);
ksort($replacements);
$content_show = preg_replace($patterns, $replacements, $content);
echo $content_show;
[/php]
and then the function UserCredit which is just above that on the page:
[php]function UserCredit($user_id) {
require ‘connect.php’;
$user_lookup = mysql_query(“SELECT * FROM prop_users WHERE ID = ‘$user_id’”);
while($row = mysql_fetch_array($user_lookup)) {
$user_name = $row[‘full_name’];
$user_file = $row[‘credit_file’];
$user_url = $row[‘link_url’];
$user_url_alt = $row[‘link_alt’];
}
if(mysql_num_rows($user_lookup)==0){
die(“An error has occured, Sorry for any Inconvenience Caused! User ID=”.$user_id);
}
require ‘close.php’;
$output = “<li class=“clearfix”><div class=“credimg”><img src=”".$user_file."" width=“75px” height=“75px” alt="".$user_name."" />
<a href="".$user_url."" title="".$user_url_alt."">".$user_name.“
Writing / Code
return $output;
}
[/php]
I have tested the function and it works when I echo UserCredit(‘1’);
And the preg_replace works at simply extracting the value for the user.
The only problem is that I can’t figure a way for the preg_replace to carry the value of $1 into the function it just literally carries “$1”
Is there something obvious that I am missing or am I going around this the wrong way?