1

I want to replace values in a string if a particular string exists in an array.

$str = 'My name is {{name}}. I live in {{city}}. I love to {{hobby}}. {{ops...}}';

$array = array(
    'name' => '010 Pixel',
    'city' => 'USA',
    'hobby' => 'code',
    'email' => '[email protected]'
);

I want to replace {{name}} with the value of name in $array. If the string inside curly brackets doesn't exist in the $array then let that string stay as it is.

Expected result:

My name is 010 Pixel. I live in USA. I love to code. {{ops...}}

The reason I'm concern about this is, when any value coming from form contains any {{field-name}} then it shouldn't get replaced. I want to replace only what is set in $str.

1
  • 1
    This is why I like StackOverflow, here we have working 4 answers with all different approaches ! Commented Feb 25, 2014 at 9:46

5 Answers 5

3

There is strtr function.

$str = 'My name is {{name}}. I live in {{city}}. I love to {{hobby}}. {{ops...}}';

$array = array(
    '{{name}}' => '010 Pixel',
    '{{city}}' => 'USA',
    '{{hobby}}' => 'code',
    '{{email}}' => '[email protected]'
);
echo strtr($str, $array);
Sign up to request clarification or add additional context in comments.

2 Comments

What if my array values are coming from $_POST?
@010Pixel Wrap {{}} to your array index should not be hard work.
1

Try this:

<?
$str = 'My name is {{name}}. I live in {{city}}. I love to {{hobby}}. {{ops...}}';

$array = array(
    'name' => '010 Pixel',
    'city' => 'USA',
    'hobby' => 'code',
    'email' => '[email protected]'
);

if (preg_match_all("/{{(.*?)}}/", $str, $m)) {
  foreach ($m[1] as $i => $varname) {
    $str = str_replace($m[0][$i], $array[$varname], $str);
  }
}

1 Comment

I have already tried this but I want some more simple way haha. Thanks :)
1
$str = 'My name is {{name}}. I live in {{city}}. I love to {{hobby}}. {{ops...}}';

$array = array(
    'name' => '010 Pixel',
    'city' => 'USA',
    'hobby' => 'code',
    'email' => '[email protected]'
);

$callback = function($match) use ($array) {
    if (array_key_exists($match[1], $array)) {
        return $array[$match[1]];
    } else {
        return $match[0];
    }
};

$str = preg_replace_callback('/\{\{(.*?)\}\}/', $callback, $str);

Comments

1

Using preg_replace_callback works-

$str = 'My name is {{name}}. I live in {{city}}. I love to {{hobby}}. {{ops...}}';
$array = array(
    'name' => '010 Pixel',
    'city' => 'USA',
    'hobby' => 'code',
    'email' => '[email protected]'
);
$res = preg_replace_callback('/\{{2}(.*?)\}{2}/',
        function($matches)use($array){
            $key = $matches[1];
            if(isset($array[$key])){
                return "{{".$array[$key]."}}";
            };
            return $matches[0];
        },
        $str);
var_dump($res);
/*
    OUTPUT-
    string 'My name is {{010 Pixel}}. I live in {{USA}}. I love to {{code}}. {{ops...}}' (length=75)
*/

Comments

0
$string = preg_replace('/{{([a-zA-Z\_\-]*?)}}/ie','$array',$str);

Hope this could help

3 Comments

Using the e modifier you can have any PHP expression in the replacement part of preg_replace.
Can you please show me a demo of this? It gives me some errors.
/e modifier is deprecated. Use preg_replace_callback instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.