using php preg_match,warning - php

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.

Related

How to match exact domain link in text with regex like "telegram.org" or etc but not "telegram.org/" subcategories

i use preg_match() function in php to find match inside string.
**$var="you can find in telegram.org/";
if(preg_match("(?=(?!(telegram\.org).*)",&var)>0)
{
echo "ok";
}else{
echo "fault";
}**
i want to print fault.
It seems you may use
if (preg_match('~^(?!.*telegram\.org/).*telegram\.org~', $var)) {
echo "OK";
}
See the regex demo.
This pattern will match any string containing telegram.org but not telegram.org/.
Details
^ - start of string
(?!.*telegram\.org/) - no telegram.org/ anywhere in the string is allowed
.* - any 0+ chars other than line break chars
telegram\.org - a telegram.org substring.
NOTE You need a regex if you want to match the telegram.org as a whole word:
'~^(?!.*\btelegram\.org/).*\btelegram\.org\b~'
Check the \b, word boundary. Else, you might consider other, non-regex ways to match the string. Like the one below, but - perhaps, better - with stripos, for a case insensitive comparison.
I recommend not using regex for this. strpos() is fast and can do this task well.
Here is a demo that only needs to call strpos() twice:
$vars=["you can find in telegram.org/","you can find in telegram.org","telegram.org here"];
$find='telegram.org';
foreach($vars as $var){
if(strpos($var,$find)!==false && strpos($var,$find.'/')===false){
echo "$find found without /\n";
}else{
echo "$find failed\n";
}
}
Output:
telegram.org failed
telegram.org found without /
telegram.org found without /
If you are set on regex, I think ~telegram\.org(?!/)~ does the job well enough. Just check for the string and see that it is not immediately followed by a /.

Match multiple delimiters containg backslashes php

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 Problem in PHP

I'm using the following regex to parse a date in dd/mm/yyyy format:
^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
I've checked it on strfriend and it all looks ok. However, when testing for it in PHP with preg_match it doesn't recognize it:
if(!preg_match("/^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$/",trim($_POST['dob']))){
$error .= "You must enter a valid date of birth.";
}
This happens with input such as 29/10/1987 and 01-01-2001, and I'm not sure why it doesn't work!
I also get the following warning:
Warning: preg_match() [function.preg-match]: Unknown modifier '.' in /home/queensba/public_html/workers/apply.php on line 18
which I'm not sure how to interpret.
If you use '/' in within your regex, you may not start/end the regular expression with it. Just replace it with '#' for example.
if(!preg_match("#^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$#",trim($_POST['dob']))){
$error .= "You must enter a valid date of birth.";
}
(BTW, Modifiers would come after the final delimiter '#'. So the warning appeared because PHP thought the regex would end after the second '/'.)
The / inside your regex trip up PHP because it thinks they are your regex delimiters.
Try
if(!preg_match("#^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$#",trim($_POST['dob']))){
$error .= "You must enter a valid date of birth.";
}
Since you are using /.../ as the pattern delimiter you need to escape all other instances of / like \/ alternative is to use a different delimiter like ~
Don't use double quotes for strings, they're for templates.
If you have '/' in your pattern, you cannot begin and end it with the same character, do it with "%","#" or any other characters...
If you have unicode string, you have to use the "u" flag.

preg_match troubles

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

simple question about preg_match() in php

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)

Categories