0

This is a simplified example, but when I run this code the first does not match as it should. It only matches the first part and nothing else. The other two occurrences match properly so I have no idea why the first wouldn't.

$str = 'What is 5 plus three?What is 4 plus three?What is 4 plus two?';

$replacees = [
'/What is (.*?) plus two\?/',
'/What is (.*?) plus three\?/',
];
$replacers = [
'I know $1 and 2.',
'I know $1 and 3.',
];

print_r( preg_replace($replacees, $replacers, $str) );

The results from that are:

I know 5 plus three?I know 4 and 3.What is 4 and 2.

But I'm expecting:

I know 5 and 3.I know 4 and 3.I know 4 and 2.

7
  • The regex engine works from left to the right. Since the first "What is" matches, .*? will match all the string until "plus two?". Commented Dec 21, 2015 at 9:27
  • you have only digits to replace ? Commented Dec 21, 2015 at 9:35
  • @casimir How is it replacing the second one then also? Commented Dec 21, 2015 at 9:37
  • @HalayemAnis No, this is just a simple example to isolate the issue. Commented Dec 21, 2015 at 9:37
  • i've got it, see my answer :) Commented Dec 21, 2015 at 9:46

2 Answers 2

2

Hi All what you did was correct just flip the code as I have shown here.

<?
 $str = 'What is 5 plus three?What is 4 plus three?What is 4 plus two?';

$replacees = [
'/What is (.*?) plus three\?/',
'/What is (.*?) plus two\?/',

];
$replacers = [
 'I know $1 and 3.',
 'I know $1 and 2.',

];

print_r( preg_replace($replacees, $replacers, $str) );

I just flipped off the code and this is working now.

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

1 Comment

While this does work, I'd rather not have to worry about positions, since my example is really a simplified one to make understanding the issue easier.
1

You should make global searching and as i see you changed last number word, so the code is:

/(?:What is ([\d])* plus (?:[\w]+)\?)/g

Of course you can make array of one, two, etc and replace it. Example:

$arrayN = array('one'=>1, 'two'=>2, 'three'=>'3');

Then change the expression like:

/(?:What is ([\d])* plus ([\w]+)\?)/g

and add to replace string:

I know $1 and $2.

All word numbers change to numbers.

3 Comments

Welcome to SO Dmitry :) +1 for your first answer, bravo :)
While that seems to be working. How would I do something similar that matches every character? For example if I had: 'What is 5five plus three?What is 4four plus three?What is 4 plus two?'
See thу text below: <? $str = 'What is 5 plus three?What is 4 plus three?What is 4 plus two?'; $arrayNymbers = array('one'=>1, 'two'=>2, 'three'=>3, 'four'=>4, 'five'=>5, 'six'=>6, 'seven'=>7, 'eight'=>8, 'nine'=>9, 'zero'=>0); $replacees = "/(?:What is ([\d])* plus ([\w]+)\?)/g"; $replacers = 'I know $1 and '.$arrayNumbers[$2].'.'; print_r(preg_replace($replacees, $replacers, $str) ); Try it

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.