sshrum Posted July 20, 2012 Share Posted July 20, 2012 Long time...re-working a script I wrote about 5 years ago. I'm trying to nest but my token word is "$nest$" and this just isn't working: $sPage = preg_replace ('$nest$', $aTemplates['body'], $aTemplates['page']); It is leaving the "$" in the page and just replacing "nest". TIA Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/ Share on other sites More sharing options...
hakimserwa Posted July 20, 2012 Share Posted July 20, 2012 use explode function to first explode all the $nest$ into an arrays of strings Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1362982 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 Long time...re-working a script I wrote about 5 years ago. I'm trying to nest but my token word is "$nest$" and this just isn't working: $sPage = preg_replace ('$nest$', $aTemplates['body'], $aTemplates['page']); It is leaving the "$" in the page and just replacing "nest". TIA The $ are acting as delimiters, which you haven't used. http://php.net/manual/en/regexp.reference.delimiters.php Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1363017 Share on other sites More sharing options...
sshrum Posted July 21, 2012 Author Share Posted July 21, 2012 I've looked over the links and still haven't figured out the correct syntax. What is the PREG expression for replacing "$nest$" in a string? Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1363367 Share on other sites More sharing options...
sshrum Posted July 21, 2012 Author Share Posted July 21, 2012 preg_quote...nvrmnd Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1363370 Share on other sites More sharing options...
sshrum Posted July 21, 2012 Author Share Posted July 21, 2012 str_replace works just the same...I've found out. Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1363371 Share on other sites More sharing options...
codefossa Posted July 21, 2012 Share Posted July 21, 2012 I think this is what you want? <?php $str = 'A $nest$ here and a $nest$ there.'; echo 'Replace strictly $nest$: ' . preg_replace('/\$nest\$/', '$new$', $str) . "\n"; echo 'Replace all within $\'s: ' . preg_replace('/\$([^\$]*)\$/', '$new$', $str) . "\n"; /* * Output of above code: * * Replace strictly $nest$: A $new$ here and a $new$ there. * Replace all within $'s: A $new$ here and a $new$ there. */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1363381 Share on other sites More sharing options...
ignace Posted July 22, 2012 Share Posted July 22, 2012 str_replace works just the same...I've found out. If both do the same then use str_replace instead of preg_replace. The former is considered much faster. Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1363409 Share on other sites More sharing options...
codefossa Posted July 23, 2012 Share Posted July 23, 2012 str_replace works just the same...I've found out. If both do the same then use str_replace instead of preg_replace. The former is considered much faster. But, if the text inside the $'s differ, it wouldn't be a realistic solution. Quote Link to comment https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/#findComment-1363597 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.