Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/265981-preg_replace-on-a-token-with/
Share on other sites

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

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.
*/

?>

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.

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.