tags:

views:

72

answers:

1

What I want to do is use preg replace to replace matches within a string with a varying replacement, and I was wondering if anyone knew if that is possible in php or at least achievable by some means. For example, a string has two matches, then those matches will be replaced with two different variables. What I want are replacements to each be a unique id and I cannot figure out how this could possibly work or if php could even do this. For example if the match is 'a' and there is a sentence, 'put a smile on a person' then one 'a' will be unique id 98aksd00 and the other will be 09alkj08. I am retrieving my comments from a database so the preg replace is happening within

while ($row=mysql_fetch_assoc($query)){
//preg replace


If anyone could provide any insight into this, I would really appreciate it

+4  A: 

You can use preg_replace_callback(), which will trigger a function call with every match:

$out = preg_replace_callback('!\bword\b!', 'replace_word', $in);

$id = 1;

function replace_word($matches) {
  global $id;
  return $id++;
}

Edit: to answer the question, this code snippet:

$message = preg_replace_callback("'\[test\](.*?)\[/test\]'",
  'replace_word' ,$message);

function replace_word() {
  return 'test';
}

The problem here is that you're using a double quoted string so you need to escape \. There's little reason to use double-quoted strings here. It also makes the syntax more confusing so:

$message = preg_replace_callback('!\[test\](.*?)\[/test\]!',
  'replace_word' ,$message);

function replace_word() {
  return 'test';
}
cletus
Thanks alot cletus I was beginning to lose hope lol. I just have one quick question to ask in case you may know the answer. I am returning my comments via a jquery/ajax/json callback and while preg_replace_callback works as it should on other pages, it seems to kill the callback of the messages. If you have any idea, I would really appreciate it, if not thanks a lot for your help.
Scarface
for example, just this will kill it
Scarface
$message = preg_replace_callback("'\[test\](.*?)\[/test\]'",'replace_word' ,$message);function replace_word() {return 'test';}
Scarface
@Scarface the problem is your double quoted strings and escapes. See the update.
cletus
Thanks for the suggestion, after more testing though, it seems that the 'return' is killing the script. Do you have any suggestions on how to some how use the function to replace its findings without using return, echo or such, since it is just being json encoded after.
Scarface
@Scarface return isn't killing the script. That's how you use `preg_replace_callback()`. You should either post here or on another question the code you're using. The error is probably in what you're returning or how you're constructing it.
cletus
Yeah as usual you are right cletus lol. I forgot to take out \\1 from my preg replace [audio\](.*?)\[/audio\] where \\1 was the middle part. In preg_replace_callback is there a way to pass into the function that same middle part? for example preg_replace_callback("'\[test\](.*?)\[/test\]'","replace_word(\\1)" ,$message);
Scarface
I promise this is my last question lol. Thank you so much for helping me out, this would have taken me hours on my own since I am relatively new to development.
Scarface
@Scarface The callback function is passed an argument. So if your expression is, for example, `\[(.*?)\]` your callback can be: `function callback($matches) { return '<' . $matches[1] . '>'; }`
cletus
Can't thank you enough cletus, you made my life much easier, I added as many points as I could to you, but I don't think it matters to you lol considering you have the most points on this site, and I am sure you are moderator anyway you could just give yourself points lol.
Scarface