1

I've a body of content that is read in and it contains numerous strings like {{some_text}} and what I'm trying to do is to find all these occurances and replace them with another value from an array for example $text["some_text"].

I've tried using preg_replace but not sure how I go about taking the found text between the brackets and use it in the replace value.

$body = "This is a body of {{some_text}} text from a book.";
$text["some_text"] = "really cool";
$parsedBody = preg_replace("\[{].*[}]/U", $text[""], $body);

As you can see I'm trying to get the some_texttext out of the string and use it to call an element from an array, this example is very basic as there $bodyvalue is vastly larger and $texthas a couple hundred elements too it.

5
  • Use preg_replace_callback. Commented Sep 2, 2015 at 21:46
  • Or maybe you should just invest in a real template library. Commented Sep 2, 2015 at 21:46
  • @Barmar, thanks for the suggestion, I'll look at preg_replace_callback now, for what I need it is overkill to use a templagte library. Commented Sep 2, 2015 at 21:47
  • An other way consists to use preg_split with the PREG_SPLIT_DELIM_CAPTURE option, to map the array and replace odd items (use an associative array) and to implode. Commented Sep 2, 2015 at 21:49
  • @Barmar, took your advice after my custom code got too complex, using twig via composer now, makes life much easier, thanks. Commented Sep 2, 2015 at 23:50

3 Answers 3

7

You can use preg_replace_callback and use the capturing group ([^}]+) to find an index in the array $text:

$repl = preg_replace_callback('/{{([^}]+)}}/', function ($m) use ($text) {
            return $text[$m[1]]; }, $body);
//=> This is a body of really cool text from a book.

The use ($text) statement passes the reference of $text to the anonymous function.

Sign up to request clarification or add additional context in comments.

Comments

4

How about doing it the other way around - instead of finding all {{...}} placeholders and looking up their values, iterate through all values and replace placeholders that match like this:

foreach ($text as $key => $value) {
    $placeholder = sprintf('{{%s}}', $key);
    $body        = str_replace($placeholder, $value, $body);
}

You can even wrap it into a function:

function populatePlaceholders($body, array $vars)
{
    foreach ($vars as $key => $value) {
        $placeholder = sprintf('{{%s}}', $key);
        $body        = str_replace($placeholder, $value, $body);
    }

    return $body;
}

Comments

0

Just for fun, using your array as is:

$result = str_replace(array_map(function($v){return '{{'.$v.'}}';}, array_keys($text)),
                      $text, $body);

Or if your array is like $text['{{some_text}}'] then simply:

$result = str_replace(array_keys($text), $text, $body);

Comments

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.