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

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.

Related

preg_match is not working properly [duplicate]

This question already has answers here:
Unknown modifier '/' in ...? what is it? [duplicate]
(4 answers)
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 9 years ago.
Regex PHP Code
// If url matches regex
$regex = "/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/";
if (preg_match($regex, $this->value)) {
$this->valid();
}
Error Message
Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier 'p' in C:\Apache\www\profiletwist\lib\php\form\url.php on line 41
Call Stack
# Time Memory Function Location
1 0.0079 440016 {main}( ) ..\new.php:0
2 0.0964 667832 form->validate( ) ..\new.php:60
3 0.0968 668248 form_URL->validateUploadURL( ) ..\form.php:372
4 0.0969 668400 preg_match ( ) ..\url.php:41
Variables in local scope (#3)
$regex =
string '/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/' (length=79)
Question
How do I fix the regex for this "unknown modifier" error to not occur?
ultimately, I would like a regex that makes sure the text input matches:
"/upload/temporary/####_##_##_[A-z0-9 _-]+ "." [a-z]{3}
This is a filename target. The beginning does not change and the last part can be a random hash followed by an arbitrary extension. Further processing is done after the regex but this is the first test.
Thank you!
In a regex string you have to escape your delimiters. Or better: use a character which doesn't appear in the regex itself as delimiter:
other delimiter (recommended):
$regex = "#^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$#";
escaped delimiters:
$regex = "/^(\/upload\/temporary\/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ \/-]+.[A-z]{2,4}$/";
You need to escape the front slashes or just use another delimiter (I've used ! in this case):
$regex = "!^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$!"
$regex = "~^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[\w./-]+\.[a-z]{2,4}$~";
Change your delimiters to ~
When you use a delimiter for example /, you must escape all litteral / in your pattern otherwhise the regex engine believes that it is the end of the pattern.
Since u is a modifier and p isn't a modifier, you have this error because of the substring /^(/up....

Getting string from html tags with php [duplicate]

This question already has answers here:
Unknown modifier '/' in ...? what is it? [duplicate]
(4 answers)
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 9 years ago.
Regex PHP Code
// If url matches regex
$regex = "/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/";
if (preg_match($regex, $this->value)) {
$this->valid();
}
Error Message
Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier 'p' in C:\Apache\www\profiletwist\lib\php\form\url.php on line 41
Call Stack
# Time Memory Function Location
1 0.0079 440016 {main}( ) ..\new.php:0
2 0.0964 667832 form->validate( ) ..\new.php:60
3 0.0968 668248 form_URL->validateUploadURL( ) ..\form.php:372
4 0.0969 668400 preg_match ( ) ..\url.php:41
Variables in local scope (#3)
$regex =
string '/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/' (length=79)
Question
How do I fix the regex for this "unknown modifier" error to not occur?
ultimately, I would like a regex that makes sure the text input matches:
"/upload/temporary/####_##_##_[A-z0-9 _-]+ "." [a-z]{3}
This is a filename target. The beginning does not change and the last part can be a random hash followed by an arbitrary extension. Further processing is done after the regex but this is the first test.
Thank you!
In a regex string you have to escape your delimiters. Or better: use a character which doesn't appear in the regex itself as delimiter:
other delimiter (recommended):
$regex = "#^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$#";
escaped delimiters:
$regex = "/^(\/upload\/temporary\/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ \/-]+.[A-z]{2,4}$/";
You need to escape the front slashes or just use another delimiter (I've used ! in this case):
$regex = "!^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$!"
$regex = "~^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[\w./-]+\.[a-z]{2,4}$~";
Change your delimiters to ~
When you use a delimiter for example /, you must escape all litteral / in your pattern otherwhise the regex engine believes that it is the end of the pattern.
Since u is a modifier and p isn't a modifier, you have this error because of the substring /^(/up....

Changing eregi in preg_match doesn't work [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
I have this code in my PHP form :
function IsEmail($email)
{
$pattern = "^([a-z0-9_]|\\-|\\.)+#(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$";
return (eregi($pattern,$email)) ? true : false;
};
And my local server gives me an error :
Function eregi() is deprecated
So I modified my code to :
function IsEmail($email)
{
$pattern = "^([a-z0-9_]|\\-|\\.)+#(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$";
return (preg_match($pattern,$email)) ? true : false;
};
and now I get this error :
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found
Any idea ?
You should begin the regexp with / and also end it with /.
Like:
$pattern = "/^([a-z0-9_]|\\-|\\.)+#(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$/";
Add a delimiter.
$pattern = "/^([a-z0-9_]|\\-|\\.)+#(([a-z0-9_]|\\-)+\\.)+[a-z]{2,7}$/";
In this case I used / as delimiter. It occurs once at the beginning and once at the end.
Note that you have to escape the delimiter character if it occurs in the regex itselfs and should not act as a delimiter but as normal matching char. So every / in your regex has to be escaped to \/ (except the delimiters of course). But you don't have any inside your regex.

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

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

PHP Regex "Unknown modifier p" [duplicate]

This question already has answers here:
Unknown modifier '/' in ...? what is it? [duplicate]
(4 answers)
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 9 years ago.
Regex PHP Code
// If url matches regex
$regex = "/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/";
if (preg_match($regex, $this->value)) {
$this->valid();
}
Error Message
Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier 'p' in C:\Apache\www\profiletwist\lib\php\form\url.php on line 41
Call Stack
# Time Memory Function Location
1 0.0079 440016 {main}( ) ..\new.php:0
2 0.0964 667832 form->validate( ) ..\new.php:60
3 0.0968 668248 form_URL->validateUploadURL( ) ..\form.php:372
4 0.0969 668400 preg_match ( ) ..\url.php:41
Variables in local scope (#3)
$regex =
string '/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/' (length=79)
Question
How do I fix the regex for this "unknown modifier" error to not occur?
ultimately, I would like a regex that makes sure the text input matches:
"/upload/temporary/####_##_##_[A-z0-9 _-]+ "." [a-z]{3}
This is a filename target. The beginning does not change and the last part can be a random hash followed by an arbitrary extension. Further processing is done after the regex but this is the first test.
Thank you!
In a regex string you have to escape your delimiters. Or better: use a character which doesn't appear in the regex itself as delimiter:
other delimiter (recommended):
$regex = "#^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$#";
escaped delimiters:
$regex = "/^(\/upload\/temporary\/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ \/-]+.[A-z]{2,4}$/";
You need to escape the front slashes or just use another delimiter (I've used ! in this case):
$regex = "!^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$!"
$regex = "~^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[\w./-]+\.[a-z]{2,4}$~";
Change your delimiters to ~
When you use a delimiter for example /, you must escape all litteral / in your pattern otherwhise the regex engine believes that it is the end of the pattern.
Since u is a modifier and p isn't a modifier, you have this error because of the substring /^(/up....

Categories