I am trying to match a Youtube URL with regex to see if it is valid. This is my code:
if(preg_match('\bhttp://youtube.com/watch\?v=.*\b', $link))
{
echo "matched youtube";
}
But I'm getting an error:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\ajax\youtube.php on line 22
I'll admit I am a complete novice to regular expressions and I don't understand them much but I am trying to learn as I do this. I made the above regex using this online regex tool:
http://gskinner.com/RegExr/
and it works there. So what am I doing wrong and is there a better way to validate a youtube URL?
Thanks. :)
There's really no need for preg_match here:
$url = "http://youtube.com/watch?v=abc";
if(strpos($url, "http://youtube.com/watch?v=") === 0) {
echo "Valid";
}
PCRE require delimiters that separate the regular expressions and optional modifiers.
In this case the \ is assumed but \ is not a valid delimiter (see error message). Use a different character like ~:
preg_match('~\bhttp://youtube\.com/watch\?v=.*\b~', $link)
You should add addition delimeters to your regexp. This is used to supply optional parameters:
preg_match('"\bhttp://youtube.com/watch\?v=.*\b"', $link)
Symbol / is usually used as regexp delimeter, but in your case it'll force inner / to be escaped. So for more clear view I suggest to use ".
When using preg_match, then the regexp needs to be enclosed with proper delimiters.
For example:
preg_match('/\bhttp://youtube.com/watch\?v=.*\b/', $link)
In your example \b stands for word boundary, this is not a valid alphanumeric delimiter, hence the error message
Related
I have a php code that needs to be matched for any of the following string using preg_match using this code
if(preg_match('/(image/gif)|(image/jpg)|(image/jpeg)/',$between))
{
echo "Match Found";
}
else
echo "Match Not Found";
but i get this error
Warning: preg_match() [function.preg-match]: Unknown modifier 'g' in C:\xampp\htdocs\project\extension.php on line 38
Any help will be appreciated....I googled alot but couldn't find solution...
You are using / as the delimiter character, so when it appears inside your regex you must escape it:
if(preg_match('/(image\/gif)|(image\/jpg)|(image\/jpeg)/',$between))
Alternatively, you can choose another delimiter:
if(preg_match('~(image/gif)|(image/jpg)|(image/jpeg)~',$between))
Replace your preg_match pattern with this:
'/(image\/gif)|(image\/jpg)|(image\/jpeg)/'
You should always escape characters like /
As long as you want / to be used inside regular expression - use ~ as a regex delimiter instead:
if(preg_match('~(image/gif)|(image/jpg)|(image/jpeg)~',$between))
^----------- ^--------
or even better:
if(preg_match('~image/(gif|jpe?g)~',$between))
regex:^([^\/])/([A-Z])([^\/]+)/([^\/]+)/$
string to match: http://127.0.0.1:8008/BeiJing/FangChan/
I use preg_match() to test
if (preg_match('/^([^\/])/([A-Z])([^\/]+)/([^\/]+)/$/',"http://127.0.0.1:8008/BeiJing/FangChan/",$match))
print $match[0];
else
print 'not match';
but get a Warning: preg_match() [function.preg-match]: Unknown modifier '('
Can anyone help me write this regex so it's valid for the preg_match function? I've tried a ton of different ideas but I'm really not too knowledgeable with regex.
/ is what is indicating the start and the end of the pattern in your example, whatever comes after the pattern is a modifier.
So this:
/^([^/])/([A-Z])([^/]+)/([^/]+)/$/
Gets read as this:
/^([^/])/
With modifiers ([A-Z])([^/]+)/([^/]+)/$/
You have to escape all /'s or change the start and end indicator:
#^([^/])/([A-Z])([^/]+)/([^/]+)/$#
You need to escape the / you use by putting a \ in front of it. Or change the first and last / to something else like:
if (preg_match('|^([^/])/([A-Z])([^/]+)/([^/]+)/$|',"http://127.0.0.1:8008/BeiJing/FangChan/",$match)) print $match[0]; else
But this regexp will not match the url.
I've been trying to use grubers latest url matching regex in a php project.
To test it I threw together something very simple:
$regex = "(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:"'.,<>?«»“”‘’]))";
$array = pret_match_all($regex, $theblockofurltext);
print_r($array);
The first problem was the " would escape a string, depending which I wrapped the regex with, so I just removed it. The use of this is personal and I will never have " anywhere near a url anyway. This left me with a new regex.
$regex = "(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))";
Raring to go I then ran my little script and it gave me the following error:
Warning: preg_split() [function.preg-split]: Unknown modifier '\' in D:\wwwroot\xxx\index.php on line 14
Unfortunately my REGEX class at school wasn't taught to anywhere near the levels of this regex requires, and I have no idea where to begin fixing this for use with PHP. Any help would be greatly appreciated. No doubt I'm probably doing something stupid too, so please go easy on me :)
Jon
Add # before and after your RE.
$regex = "#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))#";
If you use PCRE, the regular expression must be enclosed in delimiters. Now, parenthesis () can also be delimiters, that is why the engine thinks, your expression is only (?i) and interprets the next \ as modifier.
You could use ~ as delimiter:
$regex = "~(?i)\b...]))~";
Update:
I don't know whether PHP supports the partial modifying of an expression with (?i). So you might have to remove this and put the modifier after the delimiter instead (you apply it to the whole expression anyway):
$regex = "~\b...]))~i";
i used bellow code to search and find if http is includes in $url address user enters
if (!preg_match("/http:///", $user_website)
but i got this error
Warning: preg_match() [function.preg-match]: Unknown modifier '/' in
i know its becuase of // of http but how work arround this !?
Instead of having to escape every / in URL regexes it's often useful to use preg_* alternative characters to mark the start/end of the pattern.
if (!preg_match("#http://#", $user_website)
The delimiter you are using / is found in the pattern as well. In such cases you can either escape the delimiter in the pattern:
if (!preg_match("/http:\/\//", $user_website)
or you can choose a different delimiter. This will keep the pattern clean and short:
if (!preg_match("#http://#", $user_website)
You can escape the slashes like the other answers mention, or alternatively you can use different delimiters, preferably characters you won't use in your regex:
preg_match('~http://~', ...)
preg_match('!http://!', ...)
And you don't really need regex for this. String matching should be enough:
if (strpos($user_website, 'http://') !== false) {
// do something
}
See: strpos()
Surely you must do
$parts = parse_url($my_url);
$parts['scheme'] will then contain the url scheme (might be 'http').
Escape / characters with \ characters.
You need to escape literal characters. Place a back-slash before your forward slashes.
http:// becomes http:\/\/
if (!preg_match("/http:\/\//", $user_website)
if (preg_match('(\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+)', '2010/02/14/this-is-something'))
{
// do stuff
}
The above code works. However this one doesn't.
if (preg_match('/\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+/u', '2010/02/14/this-is-something'))
{
// do stuff
}
Maybe someone could shed some light as to why the one below doesn't work. This is the error that is being produced:
A PHP Error was encountered
Severity: Warning
Message: preg_match()
[function.preg-match]: Unknown
modifier '\'
Try this: (delimit the regex with ())
if (preg_match('#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#', '2010/02/14/this-is-something'))
{
// do stuff
}
Edited
The modifier u is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32.
Also as nvl observed, you are using / as the delimiter and you are not escaping the / present in the regex. So you'lll have to use:
/\p{Nd}{4}\/\p{Nd}{2}\/\p{Nd}{2}\/\p{L}+/u
To avoid this escaping you can use a different set of delimiters like:
#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#
or
#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#
As a tip, if your delimiter is present in your regex, its better to choose a different delimiter not found in the regex. This keeps the regex clean and short.
In the second regex you're using / as the regex delimiter, but you're also using it in the regex. The compiler is trying to interpret this part as a complete regex:
/\p{Nd}{4}/
It thinks the next character after the second / should be a modifier like 'u' or 'm', but it sees a backslash instead, so it throws that cryptic exception.
In the first regex you're using parentheses as regex delimiters; if you wanted to add the u modifier, you would put it after the closing paren:
'(\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+)u'
Although it's legal to use parentheses or other bracketing characters ({}, [], <>) as regex delimiters, it's not a good idea IMO. Most people prefer to use one of the less common punctuation characters. For example:
'~\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+~u'
'%\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+%u'
Of course, you could also escape the slashes in the regex with backslashes, but why bother?