473,408 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

preg_match question

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 == preg_match("/^(http.+)|(\/[a-z].+/)|(\.\.[a-z]+)/i",$string)

But I get a:

Warning: Unknown modifier ')' in myscript.php at line nn

What's wrong with the pattern?

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
Sep 17 '06 #1
2 2303

Jørn Dahl-Stamnes wrote:
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 == preg_match("/^(http.+)|(\/[a-z].+/)|(\.\.[a-z]+)/i",$string)

But I get a:

Warning: Unknown modifier ')' in myscript.php at line nn

What's wrong with the pattern?

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
If that is your exact line of code, then you missed escaping a slash
(trailing slash in second matching group, after the plus sign):

.... |(\/[a-z].+/)| ...

So, when parser reaches that slash, it believes the regex itself is now
complete and tries to interpret everything after it as modifiers. Since
you have closed brace after the unescaped slash, and it is not a valid
regex modifier, it gives you the "unknown modifier" error.

If you find yourself working with regular expressions a lot, or if you
find them hard to understand or to work with, you may want to try
something like Regex Coach (http://weitz.de/regex-coach/), or a similar
tool. It helps me a lot, especially when things get complicated.

Sep 17 '06 #2
malatestapunk wrote:
>
Jørn Dahl-Stamnes wrote:
>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 == preg_match("/^(http.+)|(\/[a-z].+/)|(\.\.[a-z]+)/i",$string)

But I get a:

Warning: Unknown modifier ')' in myscript.php at line nn

What's wrong with the pattern?

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/

If that is your exact line of code, then you missed escaping a slash
(trailing slash in second matching group, after the plus sign):

... |(\/[a-z].+/)| ...
Yes, I found that extra / in the expression some time after I posted my
message (typical :-)

Thanks.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
Sep 17 '06 #3

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...
1
by: thenetflyer | last post by:
<?php /* Using a function to perform an operation to an occurrence before returning it to the replacement string does not work in preg_replace. Consider the following code in which I want to...
2
by: sclemens420 | last post by:
On the PHP site on the preg_replace() function page ( http://us3.php.net/manual/en/function.preg-replace.php ), example 1 uses "@" signs in the regular expression members of the "search" array. On...
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",...
3
by: monomaniac21 | last post by:
hi all can anyone tell me, does this condition evaluate to true or false? $foo = ereg('ca?t', 'caaaaaaat'); thanks marc
3
by: fienen | last post by:
I am working on a script to handle a search query. In some instances, the query could come through as "isbn:%20#############" (where %20 is an encoded space and the colon is optional). Basically...
2
by: runway27 | last post by:
i am helping a friend to build a forum website which uses php and mysql database. i am working on the registeration page for the forum website and its validation. i am using php 5.2.5 i am able to...
2
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.