3

This is the program:

$string = 'Inquiry {{inquiry:number}} is assigned to {{details_1}}';
$patterns = array();
$patterns[0] = '/({{)(.*)(}})/U';
$patterns[1] = '/({{)(.*)(}})/U';
$replacements = array();
$replacements[1] = 15;
$replacements[0] = 20;
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

It should give the output:

Inquiry 20 is assigned to 15

But what I am getting:

Inquiry 20 is assigned to 20

I was wondering is it a problem with preg_replace?

Note: I am trying to replace the string inside {{ .. }} including the curly brackets, with the corresponding values.

3 Answers 3

3

By default, preg_replace replace all occurrences. Simply add optional parameter “limit” to your script and it will work:

echo preg_replace( $patterns, $replacements, $string, 1 );
Sign up to request clarification or add additional context in comments.

3 Comments

This solution seems like the best one for me. Limiting each match occurrences to 1 will do exactly what I am trying to achieve with this. Thanks!
@Pranay You may want to consider doing what the other answers say and explicitly put the text in you want to match. That way if you ever change the order of the text or remove one you don't botch your whole script up.
@Mike: I can not put the replacement texts explicitly, as they are generated dynamically. My string here is one of the many possible cases. Still, thanks for the suggestion.
3
$replArray = array(
  '{{inquiry:number}}',
  '{{details_1}}'
);

$valueArray = array (15,20);
echo str_replace($replArray,$valueArray,$patern);

If you want use the template you need parsing the variable, and checked right queue, for example {} - this right , {{} - don't right.

1 Comment

Unfortunately, I can not put the replacement texts explicitly, as they are generated dynamically. My string here is one of the many possible cases. I have not mentioned it here to keep the post short. Still, thanks for the suggestion.
2

That's because the replacements are processed one after each other and your pattern #0 is matching everything inside {{}}, so both the placeholders, and they get replaced with your replacement #0.

When the pattern #1 is processed, there are no more {{}} left.

Try this:

$string = 'Inquiry {{inquiry:number}} is assigned to {{details_1}}';
$patterns = array();
$patterns[0] = '/({{)(inquiry:number)(}})/U';
$patterns[1] = '/({{)(details_1)(}})/U';
$replacements = array();
$replacements[1] = 15;
$replacements[0] = 20;
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

1 Comment

Unfortunately, I can not put the replacement texts explicitly, as they are generated dynamically. My string here is one of the many possible cases. I have not mentioned it here to keep the post short. Still, thanks for the suggestion.

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.