BradleySmith Posted December 7, 2010 Share Posted December 7, 2010 Hey, I am trying to write a sort of replace function where {user:1} is converted into a set of information from a database using preg_replace. So I have created the following preg_replace: $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; and then the function UserCredit which is just above that on the page: 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."\" /></div><p><strong><a href=\"".$user_url."\" title=\"".$user_url_alt."\">".$user_name."</a></strong><br /><em>Writing / Code</em></p></li>"; return $output; } 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? Link to comment https://forums.phpfreaks.com/topic/220967-using-preg_replace-with-a-function/ Share on other sites More sharing options...
BlueSkyIS Posted December 7, 2010 Share Posted December 7, 2010 single quotes means literal string $replacements[1] = UserCredit($1); Link to comment https://forums.phpfreaks.com/topic/220967-using-preg_replace-with-a-function/#findComment-1144183 Share on other sites More sharing options...
BradleySmith Posted December 7, 2010 Author Share Posted December 7, 2010 Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' It's calling to the variable $1 which is invalid, and I don't want to call the variable, I want to use the preg_replace value. Link to comment https://forums.phpfreaks.com/topic/220967-using-preg_replace-with-a-function/#findComment-1144185 Share on other sites More sharing options...
AbraCadaver Posted December 7, 2010 Share Posted December 7, 2010 single quotes means literal string $replacements[1] = UserCredit($1); Actually, in this case the $1 is a regex backreference so it would be OK. Here is the problem: When you do this, usercredit() is immediately called and sent the literal string '$1', so it will always be the same: $replacements[1] = UserCredit('$1'); What you want to do is have literal PHP code assigned and then use the e modifier in your pattern to actually have preg_replace() execute the code with the substituted backreference: $patterns[1] = '/\{user:([0-9]+)\}/e'; $replacements[1] = 'UserCredit("$1")'; You can also look at preg_replace_callback(). Link to comment https://forums.phpfreaks.com/topic/220967-using-preg_replace-with-a-function/#findComment-1144187 Share on other sites More sharing options...
BradleySmith Posted December 7, 2010 Author Share Posted December 7, 2010 Thank You so much! It works. Link to comment https://forums.phpfreaks.com/topic/220967-using-preg_replace-with-a-function/#findComment-1144189 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.