kat32 Posted April 16, 2009 Share Posted April 16, 2009 what's wrong with this <?php $msg2 = preg_replace("/\{(\w+)\}/e", "\$\\1", $msg); ?> please check, thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/154299-preg_replace/ Share on other sites More sharing options...
laffin Posted April 16, 2009 Share Posted April 16, 2009 No clue wut yer trying to do ya have to provide samples and wuts wrong with it vague questions get no answers Quote Link to comment https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-811222 Share on other sites More sharing options...
laffin Posted April 16, 2009 Share Posted April 16, 2009 Before I forget U can test yer preg_stuff here online REGex TESTER v1.5.3 - test/validate regular expressions,... Quote Link to comment https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-811230 Share on other sites More sharing options...
kat32 Posted April 16, 2009 Author Share Posted April 16, 2009 it says Fatal error: preg_replace(): Failed evaluating code: Quote Link to comment https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-811234 Share on other sites More sharing options...
laffin Posted April 16, 2009 Share Posted April 16, 2009 Vague examples again yer code shows yer preg patterns yet no example of wut the variables are, $msg I expanded yer code to this <?php header('Content-type: text/plain'); //$alpha = 'apple'; //$beta = 'bob'; echo $msg = "{alpha} {beta} clone\n"; echo $msg2 = preg_replace("/\{(\w+)\}/e", "\$\\1", $msg); ?> When I tried it I got this /** {alpha} {beta} clone Notice: Undefined variable: alpha in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1 Call Stack: 0.2825 57800 1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0 0.2826 58144 2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 0.2828 59472 3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 Notice: Undefined variable: beta in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1 Call Stack: 0.2825 57800 1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0 0.2826 58144 2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 0.5247 59696 3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 clone PHP Notice: Undefined variable: alpha in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1 PHP Stack trace: PHP 1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0 PHP 2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 PHP 3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 PHP Notice: Undefined variable: beta in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1 PHP Stack trace: PHP 1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0 PHP 2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 PHP 3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4 */ So I added $alpha and $beta variables (uncomment from code sample) and then i got {alpha} {beta} clone apple bob clone so I dunno the problem Quote Link to comment https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-811243 Share on other sites More sharing options...
nrg_alpha Posted April 16, 2009 Share Posted April 16, 2009 <?php header('Content-type: text/plain'); //$alpha = 'apple'; //$beta = 'bob'; echo $msg = "{alpha} {beta} clone\n"; echo $msg2 = preg_replace("/\{(\w+)\}/e", "\$\\1", $msg); ?> You don't need to escape the { and } characters (this is a common misconception.. under rare circumstances you would, but not in this case). Nor does the dollar sign need escaping... $alpha = 'apple'; $beta = 'bob'; echo $msg = "{alpha} {beta} clone\n"; echo $msg2 = preg_replace("/{(\w+)}/e", "$\\1", $msg); Output: {alpha} {beta} clone apple bob clone Same difference though.. EDIT - Note to OP, please provide more information (as laffin mentions.. your example is vague). Quote Link to comment https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-811259 Share on other sites More sharing options...
kat32 Posted December 18, 2009 Author Share Posted December 18, 2009 thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-979670 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.