1

Why would this code

 $my_replacements = array("my dog", "doga");
 $my_string = "doga my dog test";
 $my_patterns = array_map(function($my_text) { return "/(^|[\n\r\f\t \.\,])" . trim($my_text) . "([\n\r\f\t \.\,]|$)/iu"; }, $my_replacements);
 $replaced_string = preg_replace($my_patterns, '', $my_string);
 echo $replaced_string;

return dogatest instead of test?

but if my_string is changed to "my dog doga test", it replaces correctly both elements in my_replacements?

What I want to accomplish is that, given a string, find all the strings that are in $my_replacements and delete them from the string. Taking into account the /u modifier and /i modifier or a preg_replace, because it can happen that the substring is in uppercase, and it has to be deleted either way.

1
  • could you please do a print_r of the my_patterns array? Also what value does $my_text has? Commented Nov 29, 2012 at 10:22

2 Answers 2

2

Because your replacements are transformed to the following regexps:

/(^|[\n\r\f\t \.\,])my dog([\n\r\f\t \.\,]|$)/iu
/(^|[\n\r\f\t \.\,])doga([\n\r\f\t \.\,]|$)/iu

After the first regex is applied to the source string "my dog doga test" - it cuts off the dog doga with spaces around it. So you get dogatest.

And after that the second regex cannot match anything, because it expects the doga to be enclosed by beginning or the end of the string, a space or punctuation. But in your case it's in the beginning of the string (true) but there is no a space, punctuation or end of the string after (false).

That's why the second regex doesn't modify the string and you get dogatest as a final result.

Straightforward solution without regexes:

 $my_replacements = array("my dog", "doga");
 $my_string = "doga my dog test";
 $replaced_string = str_replace($my_replacements, '', $my_string);
 echo $replaced_string;

The kind @Asad created a codepad demo of the script: http://codepad.org/ywbZR1i8

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

11 Comments

Oh! Well, how could I solve that? Why are my replacements transformed to that? I basically want to find any words that are in my replacements inside my string...
@Hommer Smith: because YOU do that with this code - $my_patterns = array_map(function($my_text) { return "/(^|[\n\r\f\t \.\,])" . trim($my_text) . "([\n\r\f\t \.\,]|$)/iu"; }, $my_replacements);
zerkms, I am trying to find a solution. What would be a good approach so I can try to find all the replacements in my array that are inside the string?
@Hommer Smith: how about str_replace? Why do you need regexes at first?
@HommerSmith Here is a demonstration, in case you want to see it working codepad.org/ywbZR1i8
|
1

Is there a reason of why you don't use str_replace? If yes, you can use
$my_replacements = array("doga", "my dog");
instead of
$my_replacements = array("my dog", "doga");

Reason: after "doga" is replaced with '' in your string, this becomes "dogatest". So, doga with spaces (as you defined the regular exp) is not there. You could also make the replace with " " instead of '',
$replaced_string = preg_replace($my_patterns, ' ', $my_string);
And then trim and replace all multiple spaces with one space (a simple recursive replace).

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.