Re: preg_match() returns false but no documentation why

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 30, 2007 3:25 pm, Jared Farrish wrote:
> On 5/30/07, Stut <stuttle@xxxxxxxxx> wrote:
>>
>> You need delimiters around the regex, as stated in the
>> documentation.
>>
>> preg_match("/^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/",$this->server)
>>
>> Although you don't need to use slashes, you can use any character
>> you
>> want but you must escape it in if it appears in the regex.
>
>
> Oh! You know, I had looked over those a couple times already. Can't
> say why
> I didn't see them.
>
> It will return false on an error, such as not having matching
> delimiters
>> aroung the regex.
>>
>> The error function may retuyrn 0, but which of the following
>> constants
>> is defined as 0?
>>
>> PREG_NO_ERROR
>> PREG_INTERNAL_ERROR
>> PREG_BACKTRACK_LIMIT_ERROR
>> PREG_RECURSION_LIMIT_ERROR
>> PREG_BAD_UTF8_ERROR
>
>
> I don't know, I'm assuming it means no error... I couldn't see
> anywhere
> where it mentioned what was what.

If you can't find them documented, print them out:

echo "PREG_NO_ERROR: '", PREG_NO_ERROR, '";

Or even:
var_dump(PREG_NO_ERROR);

> Now that I'm looking at it again, I see it's 5.2 or greater, and I
> think
> we're on 5.1 or something. Although, it seems like it would have a
> fatal
> error if I call a function that doesn't exist...

The function exists.

The constants may not...

>> Use === to distinguish FALSE from 0, which are not the same.
>
> I realize they're not the same. What I was saying was that "false" is
> not
> the stated return value if it's not found. If it's not printing a
> zero,
> shouldn't that mean it's returning false?
>
> preg_match("/^ldap(s)?:\/\/([a-zA-Z0-9-])+\.[a-zA-Z.]{2,5}$/",$this->server)

Try using | instead of / for your delimiter, so that you don't have to
dink around with escaping the / in the pattern...

Makes the code less cluttered and more clear.

Unless you have | in your pattern, or need the Regex branch '|'
character.

> Now when I add the slashes, I get zero, even though I give it a real
> value
> that should return 1. *sigh*

You may want \\. for the . in dot com

Download and play with "The Regex Coach"

It does pretty color syntax highlighting of the target string and your
regex to show you what's going on, as well as a slow-motion instant
replay to "step" through it piece by piece.

Doesn't always match PHP perfectly, as it's for Perl, but, man, that
tool alone has saved me a few hundred hours.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux