Connecting Tech Pros Worldwide Help | Site Map

preg_match not working

  #1  
Old March 6th, 2006, 11:05 PM
Andrew Richardson
Guest
 
Posts: n/a
Apologies if this has been asked before - I can't find anything on Google or
Google Groups.

I am running PHP 5.0.4 on Apache 2.0.54 with the PCRE extension installed.
For some reason though, the preg_match function is recognised by PHP but it
won't seem to accept any regexes as arguments.

For example:

$result = preg_match("[0-9][0-9]","12")

gives me an error of: Unknown modifier '['

and

$result = preg_match("hello","hello")

gives me an error of: Delimiter must not be alphanumeric or backslash

Am I doing something wrong here?

Thanks in advance

Andrew Richardson
  #2  
Old March 6th, 2006, 11:15 PM
p
Guest
 
Posts: n/a

re: preg_match not working


[color=blue]
> $result = preg_match("[0-9][0-9]","12")
>
> gives me an error of: Unknown modifier '['
>
> and
>
> $result = preg_match("hello","hello")
>
> gives me an error of: Delimiter must not be alphanumeric or backslash[/color]

You still need to wrap the regex in slashes I believe:

$result = preg_match("/[0-9][0-9]/","12")
$result = preg_match("/hello/","hello")

Hope that helps,
Pete
  #3  
Old March 6th, 2006, 11:15 PM
Andrew Richardson
Guest
 
Posts: n/a

re: preg_match not working


p wrote:
[color=blue]
> You still need to wrap the regex in slashes I believe:
>
> $result = preg_match("/[0-9][0-9]/","12")
> $result = preg_match("/hello/","hello")
>
> Hope that helps,
> Pete[/color]

Ah, that would be it. I feel a bit silly now! :)

Thanks!
  #4  
Old March 6th, 2006, 11:25 PM
Michael Winter
Guest
 
Posts: n/a

re: preg_match not working


On 06/03/2006 22:56, Andrew Richardson wrote:
[color=blue]
> $result = preg_match("[0-9][0-9]","12")
>
> gives me an error of: Unknown modifier '['
>
> and
>
> $result = preg_match("hello","hello")
>
> gives me an error of: Delimiter must not be alphanumeric or backslash
>
> Am I doing something wrong here?[/color]

You aren't including delimiters. The first character in a pattern will
be considered to be a pattern delimiter. A closing delimiter marks the
end of the pattern and the start of the flags.

In the first example above, the opening bracket is treated as the
delimiter and the next unescaped closing bracket will end the pattern.

In the second example, there are no delimiters at all.

There are various characters that can be used. Braces and brackets
(round, square, or angle), are considered in pairs. That is, the opening
bracket marks the start of the pattern, and the closing bracket ends it.
Any other non-alphanumeric or backslash character is used twice; the
same character starts and ends the pattern.

A common delimiter is the forward slash:

$result = preg_match('/[0-9][0-9]/', '12');

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
  #5  
Old March 6th, 2006, 11:25 PM
Andrew Richardson
Guest
 
Posts: n/a

re: preg_match not working


Michael Winter wrote:
[color=blue]
> On 06/03/2006 22:56, Andrew Richardson wrote:[/color]

<snip>
[color=blue][color=green]
>> Am I doing something wrong here?[/color]
>
> You aren't including delimiters. The first character in a pattern will
> be considered to be a pattern delimiter. A closing delimiter marks the
> end of the pattern and the start of the flags.[/color]

<snip>
[color=blue]
> Hope that helps,
> Mike[/color]

Thanks Mike, I'd done regexes using preg_match before and forgot about the
delimiters. Now all I have to do is get my regex to work. :)
  #6  
Old March 6th, 2006, 11:45 PM
Toby Inkster
Guest
 
Posts: n/a

re: preg_match not working


Andrew Richardson wrote:
[color=blue]
> $result = preg_match("[0-9][0-9]","12")
>
> gives me an error of: Unknown modifier '['[/color]

$result = preg_match("/[0-9][0-9]/","12");

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
addslashes, mysql_real_escape_string, etc not working pedalpete answers 8 May 13th, 2008 10:40 PM
preg_matching an XML node not working jsavagedesign answers 1 January 8th, 2008 06:39 PM
{SOLVED} PHP script not working properly. aaronic answers 1 November 14th, 2006 03:59 PM
Check $photo size and extension not working Philip D Heady answers 5 July 17th, 2005 03:54 AM