What's wrong with my code preg_match? [duplicate] - php

This question already has answers here:
PHP regular expressions: No ending delimiter '^' found in
(2 answers)
Closed 8 years ago.
While running this regex:
if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){`
I get the following error message:
Warning: preg_match(): No ending delimiter '^' found in C:\xampp\htdocs\Test\source\index.php on line 28.
Why?

preg_match() thinks you're using the carat "^" as the delimiter. Generally, you'll find most people use "/".
if(!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/", $email)) {

Related

PHP- Warning: preg_match(): Unknown modifier '(' [duplicate]

This question already has answers here:
Unknown modifier '(' when using preg_match() with a REGEX expression [duplicate]
(5 answers)
Closed 4 years ago.
I am checking browser agent and version using preg match. but i found error while i update my php version 5.3 to 5.4
preg_match( '/Mozilla/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version)
have any idea how to fix this issue?
Apply \ (backslash) before second /(forwardslash) to escape it
preg_match( '/Mozilla\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version);
preg_match('/Netscape([0-9])\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version1);
Note:- Any / in between starting and ending /(delementer) in preg_match() need to be escaped to make it run fine.

PHP Preg_match() Unknown Modifier Url [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I got this error message:
Warning: preg_match(): Unknown modifier '/' in C:\xampp\htdocs\easyblog\wp-content\themes\easyblog\inc\widget\flickr.php on line 225
Source code in here :
// Screen name or RSS in $username?
if (!preg_match("/http://api.flickr.com/services/feeds/", $username))
$url = "http://api.flickr.com/services/feeds/photos_public.gne?id=".urlencode($user_id)."&format=".$flickrformat."&lang=en-us".$tags;
else
$url = $username."&format=".$flickrformat.$tags;
Can someone help me, please!
You need to escape the forward slashes in your expression:
"/http:\/\/api.flickr.com\/services\/feeds/"
When you use / delimiter, then you must escape all / character if used inside pattern, for avoiding this you can use another delimiter such as #
"#http://api.flickr.com/services/feeds#"
However, your pattern needs escaping:
"/http:\/\/api.flickr.com\/services\/feeds/"

regex error preg_replace_callback() : Unknown modifier '.' in [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I got the following string:
last_name, first_name
bjorge, philip
kardashian, [kghim]
mer#$##Code:menu:51587daa7030e##$#cury some more
data #$##Code:menu:515r4387daa7dsf030e##$#, freddie
im trying to replace the Codes in the middle with the function: 'codeParser' the regex is:
$PC_File = preg_replace_callback("(?=\#\$\#\#).*?(?<=\#\#\$\#)", 'codeParser', $PC_File);
but getting this error:
PHP Warning: preg_replace_callback() : Unknown modifier '.'
You need to wrap your regular expression in delimiters. It's considering () to be the delimiters right now, and the . as a modifier (which is of course invalid).
"/(?=#\\$##).*?(?<=##\\$#)/"
(I'm also pretty sure the # do not need to be escaped unless you were using them as delimiters)
EDIT: You need \\ to properly escape the $ in double-quotes.

preg_match php error - No ending delimiter '^' found [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php regular expressions No ending delimiter ‘^’ found in
The code is as follows:
if(preg_match($exp, $value)){
return "";
}
the value of $exp is: ^[0-9]*$
the value of $value is: 7
and the output gives :
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /...
$exp should be something like /^[0-9]*$/ for it to work the way you intended. It is assuming ^ is your regex delimiter, because it is the first character - you'd want your first char to be something else. See preg_match manual entry for details.

PHP preg_replace error: Unknown modifier ']' [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
Apologies for yet another preg_replace question!
I'm getting the error:
Warning: preg_replace() [function.preg-replace]: Unknown modifier ']' in C:\xampp\htdocs\reg.php on line 6
When running the following preg_replace:
$newContent = preg_replace('<img [^>]*.gif[^>]*?>','MATCHED',$newContent);
I've run the regex through a couple of online regex builders and it seems sound. Any ideas what I'm missing here? Am I using ] incorrectly?
You need a delimiter around your expression:
$newContent = preg_replace('|<img [^>]*.gif[^>]*?>|','MATCHED',$newContent);

Categories