Search code examples
regexpreg-replacephp-5.2

preg_replace exact opposite


I'm looking to do something like get the opposite match from this line:

$input = "21.asdf*234true;asdf0--11"
$_BOOL_ ="/(true|false)/i";
$output = preg_replace($_BOOL_, '', $input);
//Result: "21.asdf*234;asdf0--11"
//Desired result: "true"

Which ofcourse in php 5.3 is

$output = preg_filter($_BOOL_, '', $input);

But I'm on 5.2, and not sure how to get what I want here... Suggestions (other than compile 5.3 on ubuntu)?


Solution

  • Your example is still a little vague.

    If the string contains EITHER true or false, the output should be "true"?

    If that is the case you are looking for preg_match() I think.

    If you are looking to return either "true" if the string contains true and "false" if it contains false, I think you may have to use a series of functions such as preg_match() or strpos()to match each condition seperately.