473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

preg_match not working

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
Mar 6 '06 #1
5 39943
p
$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


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
Mar 6 '06 #2
p wrote:
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


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

Thanks!
Mar 6 '06 #3
On 06/03/2006 22:56, Andrew Richardson wrote:
$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?


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.
Mar 6 '06 #4
Michael Winter wrote:
On 06/03/2006 22:56, Andrew Richardson wrote:
<snip>
Am I doing something wrong here?


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.


<snip>
Hope that helps,
Mike


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. :)
Mar 6 '06 #5
Andrew Richardson wrote:
$result = preg_match("[0-9][0-9]","12")

gives me an error of: Unknown modifier '['


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

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

Mar 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out...
4
by: DH | last post by:
I need to parse some HTML tags and display the style classes, and have it partly working, but need some regex / preg_match advise. If the tag is <td class="red" colspan="1"> I can display...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
2
by: Jørn Dahl-Stamnes | last post by:
I try to use preg_match to find out if a string begins with either "http", "/somethime" or "../something". In my php script I have the following line: if (0 ==...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
0
by: Ja NE | last post by:
I would like to offer my registered users to have very short uri for their pages in our comunity site, so I have enabled wildcard dns *.bellay.com. and added virtaul host for it. (I'm testing this...
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.