Jump to content

preg_replace problem


BrettCarr

Recommended Posts

Hi Guys,

I have a small issue with doing a preg_replace on a string.

Here is the string I wish to use preg_replace on

 

 

<?php
$mystring = "This is my test string that has {['site_info']} and some more stuff {['taste']} then nothing"
?>

 

 

I want to do a preg_replace and replace {['site_info']} with  $stuff['major']['site_info'] through the string

I want to replace the placeholder with real variables from the array

Any help would be great

 

 

Link to comment
https://forums.phpfreaks.com/topic/248865-preg_replace-problem/
Share on other sites

<?php

$string = "This is my test string that has {['site_info']} and some more stuff {['taste']} then nothing";
$string = preg_replace('/\{\[\'site_info\'\]\}/', '$stuff[\'major\'][\'site_info\']', $string);

echo $string;

?>

 

Will echo this:

 

This is my test string that has $stuff['major']['site_info'] and some more stuff {['taste']} then nothing

 

But if you wanted to insert a variable rather than actually put the text in there like that:

 

<?php

$stuff['major']['site_info'] = 'VARIABLE!!';

$string = "This is my test string that has {['site_info']} and some more stuff {['taste']} then nothing";
$string = preg_replace('/\{\[\'site_info\'\]\}/', $stuff['major']['site_info'], $string);

echo $string;

?>

 

Will echo this:

 

This is my test string that has VARIABLE!! and some more stuff {['taste']} then nothing

 

Hope that answers your problem.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.