dataxa Posted February 27, 2007 Share Posted February 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted February 27, 2007 Share Posted February 27, 2007 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 Quote Link to comment Share on other sites More sharing options...
dataxa Posted February 27, 2007 Author Share Posted February 27, 2007 if i just put this: echo ereg_replace("(@{2})(.{1,10})(@{2})","\\2",$text); then i get: Fish swims in a rep1 Fish lives in a rep2 so it works, just i dont know how i can tell php to accept them as variables Quote Link to comment Share on other sites More sharing options...
btherl Posted February 27, 2007 Share Posted February 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
dataxa Posted February 28, 2007 Author Share Posted February 28, 2007 $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); Quote Link to comment Share on other sites More sharing options...
effigy Posted February 28, 2007 Share Posted February 28, 2007 An array would be better, but here you go: <pre> <?php $rep1 = "water"; $rep2 = "aquarium"; $text = "Fish swims in a @@rep1@@. Fish lives in a @@rep2@@."; echo preg_replace("/@@([^@]{1,10})@@/e", '$\1', $text); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.