Re: [PHP] preg_match() returns false but no documentation why



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



Relevant Pages

  • Re: [PHP] Removing UTF-8 from text
    ... http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php lists what the modifiers do. ... you can use anything as delimiters. ... Sometimes it's easier to change the delimiter rather than worrying about escaping it - for example when doing a regex for urls.. ...
    (php.general)
  • Re: parse website _cliptext for name, email, web URL, phone, cellphone, street, city, zip
    ... >the text up with the CHRdelimiters and then use regex to match the ... _cliptext came from this particular web site and that would determine ...
    (microsoft.public.fox.programmer.exchange)
  • Re: String split regex question
    ... Why don't you use a StringTokenizer for this, ... delimiters (not a regex). ... [don't email me support questions or followups] ...
    (comp.lang.java.programmer)
  • Re: Regex error
    ... Get The Regex Coach http://weitz.de/regex-coach/ ... Build your pattern one step at a time using the coach. ... Don't forget the delimiters. ...
    (php.general)
  • Re: Hub transport regex is broken, a horrible implementation, or Im an idiot.
    ... grep, egrep, and perl - all of which use a standard regex ... occurrences of the preceding character. ... I don't know which regex engine they use, ... organization' predicate, I can already tell you it won't work thanks ...
    (microsoft.public.exchange.admin)