Re: Regular expressions, filter option1 OR option2
| From: | Camilo Sperberg | Date: | Wed, 18 Aug 2010 19:53:30 +0000 |
| Subject: | Re: Regular expressions, filter option1 OR option2 | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-307543@lists.php.net to get a copy of this message | ||
On Wed, Aug 18, 2010 at 15:01, Ashley Sheridan <ash@ashleysheridan.co.uk>wrote:
> On Wed, 2010-08-18 at 23:36 +0530, Shreyas Agasthya wrote:
>
> Camilo,
>
> What exactly are you trying to achieve? Meaning:
>
> if (true)
> do this;
> if (false)
> do that;
>
> However, here's a link that I used long back to help me with some RegEx :D](�HÎ;ù
> ½http://www.gskinner.com/RegExr/
>
> Regards,
> Shreyas
>
> On Wed, Aug 18, 2010 at 11:31 PM, Camilo Sperberg <csperberg@unreal4u.com>wrote:
>
> > Hello list :)
> >
> > Just a short question which I know it should be easy, but I'm no expert yet
> > in regular expressions.
> > I've got a nice little XML string, which is something like this but can be
> > changed:
> >
> > <?xml version="1.0" encoding="utf-8"?>
> > <boolean
> > xmlns="http://tempuri.org/">false</boolean>
> >
> > The boolean value can be true or false, so what I want to do, is filter it.
> > I've done it this way, but I know it can be improved (just because I love
> > clean coding and also because I want to master regular expressions xD):
> >
> > $result = preg_match('/true/',$curl_response);
> > if ($result == 0) $result = preg_match('/false/',$curl_response);
> >
> > I've also tried something like:
> > $result = preg_replace('/^(true)|^(false)/','',$curl_response); // if
> > not
> > true OR not false => replace with empty
> >
> > and also '/^true|^false/' which doesn't seem to work.
> >
> > Any ideas to filter this kind of string in just one expression?
> >
> > Thanks in advance :)
> >
> > --
> > Mailed by:
> > UnReAl4U - unreal4u
> > ICQ #: 54472056
> > www1: http://www.chw.net/
> > www2: http://unreal4u.com/
> >
>
>
>
>
>
> As far as I can tell, are you just trying to grab the content from the
> <boolean> tag text node? If it's limited to this basic example, there's
> likely not much of a need to use dedicated DOM functions, but consider using
> them if you need to work on more complex documents.
>
> If you are just trying to determine if the value is indeed true or false
> and nothing else, then a regex could be overkill here. As you only need to
> check two values, consider:
>
> if(strpos($xml, 'true') || strpos($xml, 'false'))
> {
> // text node content is OK
> }
> else
> {
> // bad, bad input, go sit in the corner
> }
>
>
Indeed Ashley, I'm trying to get the TRUE / FALSE value from the boolean
tag.
After reading again, you're absolutely right: strpos does just what I need
it to do: a simple search for a true or false. (Or null, in which case
strpos will be FALSE anyway).
Thanks for your suggestion, I was doing an overkill here.
@Shreyas: thanks for the link! It is very helpful to speed up the testing a
bit!
Greetings!
--
Mailed by:
UnReAl4U - unreal4u
ICQ #: 54472056
www1: http://www.chw.net/
www2: http://unreal4u.com/