Using preg_match to Find URL [duplicate] - php

This question already has answers here:
Warning: preg_match() [function.preg-match]: Unknown modifier '/' [duplicate]
(2 answers)
Closed 8 years ago.
I've been trying to use preg_match to find a form with a certain action URL.
Here the code
if (!preg_match('/<form method="post"
action="https[\:]//www.amazon.com/gp/aw/si.html/ref=aw_c_co".*?<\/form>/is', $page, $form)) {
die('Failed to CHECKOUT form!');
But I get an error message:
Warning: preg_match(): Unknown modifier '/'
I dont understand whats wrong.
Thanks

You have to escape (\) the / since these are your regex delimiters! Maybe you want to use other delimiters?
/ -> \/

You can use the following code :
$str = '<form method="post" action="https[\:]//www.amazon.com/gp/aw/si.html/ref=aw_c_co".*?<\/form>';
$str = preg_replace('^.*<form.*?action="(.+?)".*$', '$1', $str);
It give you : https[\:]//www.amazon.com/gp/aw/si.html/ref=aw_c_co
Good luck

Related

preg_match(): Unknown modifier 'G' in testscripts [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
foreach ($expected_lines as $expected_line) {
if (!preg_match("/$expected_line\s*/", $contents)) {
$match = false;
$unmatched = $expected_line;
break;
}
}
This is my code which give me error
PHPUNIT_FRAMEWORK_ERROR_WARNING
preg_match(): Unknown modifier 'G'
Let me know where i m gone wrong
It seems that the regular expression passed to preg_match is incorrect after actual value of $expected_line is used in "/$expected_line\s*/".
Everytime you use data you got from users in regular expressions, you need to properly escape it to avoid situations like that - when characters provided by user break the expression. Use preg_quote for that:
$escaped_expected_line = preg_quote($expected_line, '/');
if (!preg_match("/$escaped_expected_line\s*/", $contents)) {
...
}
Check here for more info: https://secure.php.net/manual/pl/function.preg-quote.php

PHP Unknown Modifier [duplicate]

This question already has answers here:
Unknown modifier '/' error in PHP [duplicate]
(5 answers)
Closed 8 years ago.
Have trouble with this and have looked back at previous answers but still
seem not to be getting it. Here is the code:
preg_replace('/^.*"([http|https]://test.co/v/.*)/embed.*width=".*$/', '$1', $str);
Am getting a Unknown modifier '/' error.
Looking at the previous answers it looks like I have to deliminate the / that is
used in the query so I added a ~ before each forward slash but still the same
error. Am guessing I didn't quite understand it... any suggestions appreciated!
Escape the / in the regex as
preg_replace('/^.*"((?:http|https):\/\/test\.co\/v\/.*)\/embed.*width=".*$/', '$1', $str);
OR
You can use a different delimter eg #
preg_replace('#^.*"((?:http|https:)//test\.co/v/.*)/embed.*width=".*$#', '$1', $str);
Note
You can shorten http|https as https?
Escape the . in .co as \.co

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/"

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

Getting a unknown modifier error when using a RegEx [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm not very knowledgeable with regular expresions, so I don't understand why I'm getting this error.
I'm using the following code to match all the emails in the string $str and saving them in the array $match:
preg_match_all(
"/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/",
$str,
$match
);
Apparently there's a problem in the regex (which I got from here) because I'm getting this error:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier
'=' in C:\xampp\htdocs\project\Framework\Sanitizer.class.php on
line 38
Can someone tell me what's the problem?
Thanks
You have to escape the forward slash before that equals sign, e.g:
preg_match_all(
"/^[a-zA-Z0-9.!#$%&’*+\/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/",
$str,
$match
);
"/" is the delimiter for the whole expression, so it must be escaped in the regex itself
You need to escape many of the characters in that string with a backslash.
Some of those you want to escape is: /?^{|} .
I would have written it like this:
preg_match_all(
"/^[a-zA-Z0-9.!#$%&’*+\/=\?\^_`\{\|\}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/",
$str,
$match
);
I find this site quite useful when it comes to matching e-mail adresses:
http://www.regular-expressions.info/email.html

Categories