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))
Related
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 /.
I'm trying to check if a string ends with one _ and two letters on an old system with php. I've check here on stackoverflow for answers and I found one that wanted to do the same but with one . and two digits.
I tried to change it to work with my needs, and I got this:
\\.*\\_\\a{2,2}$
Then I went to php and tried this:
$regex = '(\\.*\\_\\a{2,2}$)';
echo preg_match($regex, $key);
But this always returns an error, saying the following:
preg_match(): Delimiter must not be alphanumeric or backslash
I get this happens because I can't use the backslashes or something, how can I do this correctly? And also, is my regex correct(I don't know ho to form this expressions and how they work)?
You can use this regex with delimiters:
$regex = '/_[a-z]{2}$/i';
You're getting that error because in PHP every regex needs a delimiter (not use of / above which can be any other character like ~ also).
^.*_[a-zA-Z]{2}$
This should do it for you.
$re = "/^.*_[a-zA-Z]{2}$/";
$str = "abc_ac";
preg_match($re, $str);
I try to match this expression :
* ^X-Spam-Flag: YES
I use this code :
if(preg_match('\'* ^X-Spam-Flag: YES\'', $value))
But i have this error :
PHP Warning: preg_match(): Compilation failed: nothing to repeat at
offset 0
Problem with regex and * and ^ but can i correct that ?
You are missing your delimiters.
Change to:
preg_match('#\'* ^X-Spam-Flag: YES\'#', $value)
Or maybe you need:
preg_match('#\* \^X-Spam-Flag: YES#', $value)
You was near the solution, you have to escape ^ and * since these have a special meaning in a regex pattern:
if(preg_match('\'\* \^X-Spam-Flag: YES\'', $value))
If your escaped single quotes look strange and may be better replaced by / or ~ ... however these can be used as delimiters.
More informations about delimiters here: http://www.php.net/manual/en/regexp.reference.delimiters.php
To safely escape Regex special characters, you can simply use "preg_quote()", which adds backslashes in front of these characters.
if(preg_match('\'* ^X-Spam-Flag: YES\'', preg_quote($value)))
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 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