views:

39

answers:

2

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)?

A: 

How about:

$output = preg_replace($_BOOL_, '', preg_grep($_BOOL_, $input));

EDIT: Looking at your question again, I'm not sure we're talking about the same thing. For each string in the input array, preg_replace performs any replacements it can and returns the result; any string that doesn't match the regex is passed along unchanged. preg_filter is the same, except it discards the strings that don't match the regex. I wouldn't call those opposites. If this isn't what you're looking for, maybe you can provide some examples.

Alan Moore
I added an example above.
+1  A: 

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.

Chris Sobolewski
I'm going to accept this as I've just realised the flaw of my thinking here. I've been using this in a wrapper to use pre-defined regex to strip charecters, but you've made me realise this allows for strings like "truefalsefalse," which I do not want. I will be using preg_match to preform what I want. Thanks.