Jump to content

ereg_replace replacement


dataxa

Recommended Posts

My aim is to replace @@something@@ parts of the texts with the same name of variables defined before ($something)

 

<?php

$rep1 = "water";

$rep2 = "aquarium";

$text = "Fish swims in a @@rep1@@. Fish lives in a @@rep2@@.";

 

echo ereg_replace("(@{2})(.{1,10})(@{2})",${"\\2"},$text);

echo "\n";

echo $text;

?>

 

script says:undefined variable '\2' in line 6, as look like construction ${} will take everything inside it between the quotas as string and one slash allows second slash to be displayed

 

as you can see actually \\2 is a second part of string what should be replaced by $rep1 or $rep2

 

I hope somebody can help me.

Link to comment
Share on other sites

You definitely can't do it like that.  There's bizarre extensions in perl which allow you to do that within a regexp, but I would do it outside to maintain sanity.

 

The simplest and cleanest way I can think of is to use one ereg_replace() for each variable you are replacing.  Then there's no need to be so flexible with the regexp.

 

If you're planning to do some heavy duty replacing (too much to use individual ereg_replace()), I would use Smarty or another existing template system.

 

And finally, preg_replace() is faster :)

Link to comment
Share on other sites

Aha, it is possible.  You need this function:

 

http://sg.php.net/manual/en/function.preg-replace-callback.php

 

Your callback will need to see which string was matched (eg rep1, rep2) and return the appropriate variable.  It could be as simple as:

 

function my_callback($matches) {
  return $$matches[0];
}

 

The variables must be available within my_callback(), so you may need to declare them as global.  Or you can return $_GLOBALS[$matches[0]] instead.  Then

 

echo preg_replace_callback("/(@{2})(.{1,10})(@{2})/","my_callback",$text);

 

No guarantees on this untested code.  Note that to use preg instead of ereg, you usually just need to add "/" to the start and end of the pattern, and escape any "/" which appears within the pattern.

Link to comment
Share on other sites

$text = "Fish swims in a @@rep1@@. Fish lives in a @@rep2@@.";

 

function callback($value) {

$rep1 = "water";

$rep2 = "aquarium";

return $$value;

}

 

echo preg_replace("/(@{2})(.{1,10})(@{2})/",callback("\\2"),$text);

echo "\n";

echo $text;

 

It still takes \\2 as \2. Somt time i got idea to use eval(), but it didnt work as well.

I mean:

preg_replace("/(@{2})(.{1,10})(@{2})/",eval("$\\2"),$text);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.