Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
preg_match() returns false but no documentation why
Old
  (#1)
Jared Farrish
Guest
 
Posts: n/a
Default preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

Hi all,

Can anybody spot why this doesn't seem to be working right? The manual (
http://us2.php.net/preg_match) says it returns "false" on error, but
preg_last_error() returns 0, which I ***ume points to the "PREG_NO_ERROR"
error code.

<code>
preg_match("^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$",$this->server)
</code>

I also tried ereg(), and have searched and gone through the comments. Why
would a regex operation return false?

That may be ugly, since I've not done a lot of regex's yet. I have checked
and $this->server does insert a valid string. What I am trying to do is
validate ldap://com.com and ldaps://com.com and all valid variations of. Is
there something wrong with the regex, or am I pumping an invalid format into
preg_match()?

Incidentally, I stole the last piece (after ldaps://) off a regex for email
addresses (from SitePoint,
http://www.sitepoint.com/article/reg...pressions-php).

Thanks!

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: [PHP] preg_match() returns false but no documentation why
Old
  (#2)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

On Wed, May 30, 2007 12:33 pm, Jared Farrish wrote:
> Hi all,
>
> Can anybody spot why this doesn't seem to be working right? The manual
> (
> http://us2.php.net/preg_match) says it returns "false" on error, but
> preg_last_error() returns 0, which I ***ume points to the
> "PREG_NO_ERROR"
> error code.
>
> <code>
> preg_match("^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$",$this->server)


You are missing the start/end delimiters is your first problem...

> </code>
>
> I also tried ereg(), and have searched and gone through the comments.
> Why
> would a regex operation return false?


It would return false if your string doesn't match the expression.

--
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?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match() returns false but no documentation why
Old
  (#3)
Jared Farrish
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

On 5/30/07, Richard Lynch <EMAIL REMOVED> wrote:
> On Wed, May 30, 2007 12:33 pm, Jared Farrish wrote:
> >
> > preg_match("^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$",$this->server)

>
> You are missing the start/end delimiters is your first problem...


Which ones? I've got the starter "^" and the closer "$", so what else am I
missing?

> would a regex operation return false?
>
> It would return false if your string doesn't match the expression.
>


The manual claims it will return a 0 signaling "0 matches found." And then,
under "Return Values," it's says very quickly:

"*preg_match()* returns *FALSE* if an error occurred."

If it's not returning ANYTHING I'm ***uming it's faulting, but the calling
the error function returns 0 (kind've ironic, really...).

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match() returns false but no documentation why
Old
  (#4)
Stut
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

Jared Farrish wrote:
> On 5/30/07, Richard Lynch <EMAIL REMOVED> wrote:
>> On Wed, May 30, 2007 12:33 pm, Jared Farrish wrote:
>> >
>> > preg_match("^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$",$this->server)

>>
>> You are missing the start/end delimiters is your first problem...

>
> Which ones? I've got the starter "^" and the closer "$", so what else am I
> missing?


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.

>> would a regex operation return false?
>>
>> It would return false if your string doesn't match the expression.
>>

>
> The manual claims it will return a 0 signaling "0 matches found." And then,
> under "Return Values," it's says very quickly:
>
> "*preg_match()* returns *FALSE* if an error occurred."
>
> If it's not returning ANYTHING I'm ***uming it's faulting, but the calling
> the error function returns 0 (kind've ironic, really...).


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

-Stut
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match() returns false but no documentation why
Old
  (#5)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM



On Wed, May 30, 2007 3:06 pm, Jared Farrish wrote:
> On 5/30/07, Richard Lynch <EMAIL REMOVED> wrote:
>> On Wed, May 30, 2007 12:33 pm, Jared Farrish wrote:
>> >
>> > preg_match("^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$",$this->server)

>>
>> You are missing the start/end delimiters is your first problem...

>
> Which ones? I've got the starter "^" and the closer "$", so what else
> am I
> missing?


Whatever character you want to use:

"|^ldap(s)?"//[a-zA-Z0-9-]*\\.[a-zA-Z.]{2,5}$|"
"%^ldap(s)?"//[a-zA-Z0-9-]*\\.[a-zA-Z.]{2,5}$%"
"#^ldap(s)?"//[a-zA-Z0-9-]*\\.[a-zA-Z.]{2,5}$#"


>> would a regex operation return false?
>>
>> It would return false if your string doesn't match the expression.
>>

>
> The manual claims it will return a 0 signaling "0 matches found." And
> then,
> under "Return Values," it's says very quickly:
>
> "*preg_match()* returns *FALSE* if an error occurred."
>
> If it's not returning ANYTHING I'm ***uming it's faulting, but the
> calling
> the error function returns 0 (kind've ironic, really...).


Use === to distinguish FALSE from 0, which are not the same.

--
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?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match() returns false but no documentation why
Old
  (#6)
Jared Farrish
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

On 5/30/07, Stut <EMAIL REMOVED> 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 ***uming it means no error... I couldn't see anywhere
where it mentioned what was what.

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

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

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

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match() returns false but no documentation why
Old
  (#7)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

On Wed, May 30, 2007 3:25 pm, Jared Farrish wrote:
> On 5/30/07, Stut <EMAIL REMOVED> 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 ***uming 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?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match() returns false but no documentation why
Old
  (#8)
Jared Farrish
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

On 5/30/07, Richard Lynch <EMAIL REMOVED> wrote:
>
> If you can't find them documented, print them out:
>
> echo "PREG_NO_ERROR: '", PREG_NO_ERROR, '";
>


Doh!

PREG_NO_ERROR: 0
PREG_INTERNAL_ERROR: 1
PREG_BACKTRACK_LIMIT_ERROR: 2
PREG_RECURSION_LIMIT_ERROR: 3
PREG_BAD_UTF8_ERROR: 4

So apparently, "PREG_NO_ERROR" is synonymous for "you need delimiters,
egghead."

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



You only have to escape "/" if it's part if it's the pattern delimiter?

Makes the code less cluttered and more clear.


Fo' sho'.


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



Ok, I tried:

preg_match("|^ldap(s)?://([a-zA-Z0-9-])+\.[a-zA-Z.]{2,5}$|",$this->server)
preg_match("|^ldap(s)?://([a-zA-Z0-9-])+\\.[a-zA-Z.]{2,5}$|",$this->server)
preg_match("|^ldap(s)?:\/\/([a-zA-Z0-9-])+\\.[a-zA-Z.]{2,5}$|",$this->server)

using: $this->server = "ldap://www.example.com";

No luck. I'll the try tool you referred to; I have been using
regular-expressions.info for information.

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.



Oooh, pretty colors! Stepping through sounds interesting. I'll have to check
it out.

Thanks!

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] preg_match() returns false but no documentation why
Old
  (#9)
Jim Lucas
Guest
 
Posts: n/a
Default Re: [PHP] preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

Stut wrote:
> Jared Farrish wrote:
>> On 5/30/07, Richard Lynch <EMAIL REMOVED> wrote:
>>> On Wed, May 30, 2007 12:33 pm, Jared Farrish wrote:
>>> >
>>> > preg_match("^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$",$this->server)
>>>
>>> You are missing the start/end delimiters is your first problem...

>>
>> Which ones? I've got the starter "^" and the closer "$", so what else
>> am I
>> missing?

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

This isn't going to work, the op has two forward slashes in the string that he is wanting to match with.

The op will need to use something other than forward slashes.

so, this is going to match:
ldap://testing123.com TRUE
ldap://www.testing-123.com FALSE
ldap://testing123.com.uk FALSE
ldap://testing123.or.us TRUE

preg_match('|^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$|', $this->server )

I also recommend using single quotes instead of double quotes here.

btw: why is there a period in the second pattern? Also, why are you allowing for uppercase letters
when the RFC's don't allow them?

Just my thoughts

>
> 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.
>
>>> would a regex operation return false?
>>>
>>> It would return false if your string doesn't match the expression.
>>>

>>
>> The manual claims it will return a 0 signaling "0 matches found." And
>> then,
>> under "Return Values," it's says very quickly:
>>
>> "*preg_match()* returns *FALSE* if an error occurred."
>>
>> If it's not returning ANYTHING I'm ***uming it's faulting, but the
>> calling
>> the error function returns 0 (kind've ironic, really...).

>
> 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
>
> -Stut
>



--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Unknown
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: preg_match() returns false but no documentation why
Old
  (#10)
Jared Farrish
Guest
 
Posts: n/a
Default Re: preg_match() returns false but no documentation why - 06-02-2007, 08:56 PM

On 5/30/07, Jim Lucas <EMAIL REMOVED> wrote:

> The op will need to use something other than forward slashes.



You mean the delimiters (a la Richard's suggestion about using '|')?

so, this is going to match:
> ldap://testing123.com TRUE
> ldap://www.testing-123.com FALSE
> ldap://testing123.com.uk FALSE
> ldap://testing123.or.us TRUE



Hmm. What makes them fail/not fail? The '//' in the pattern?

preg_match('|^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$|', $this->server )
>
> I also recommend using single quotes instead of double quotes here.



Single Quotes: Noted. Any reason why? I guess you might be a little out of
luck putting $vars into a regex without . concatenating.

> why is there a period in the second pattern?


The period comes from the original article on SitePoint (linked earlier). Is
it unnecessary? I can't say I'm real sure what this means for the '.' in
regex's:

"Matches any single character except line break characters \r and \n. Most
regex flavors have an option to make the dot match line break characters
too."
- http://www.regular-expressions.info/reference.html

> Also, why are you allowing for uppercase letters
> when the RFC's don't allow them?


I hadn't gotten far enough to strtolower(), but that's a good point, I
hadn't actually considered it yet.

Just my thoughts


Hey, I appreciate it!

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




Contact Us - Forum Care Forums - Archive - Top