PHP Unknown Modifier [duplicate] - php

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

Related

PHP Trouble replacing string containing dash and underscore [duplicate]

This question already has answers here:
PHP regular expressions: No ending delimiter '^' found in
(2 answers)
Closed 6 years ago.
I have tried, read and searched to to avail.
I just need to replace -m_ with -b_ in a string containing an image name / location.
For instance:
I just want to replace;
some-image-name-hyphenated-m_15235101.jpg
with
some-image-name-hyphenated-b_15235101.jpg
Easy right?
$mediumimage = 'some-image-name-hyphenated-b_15235101.jpg';
I tried
$biggerimage = preg_replace("-m_", "-b_", $mediumimage);
and
$biggerimage = preg_replace('-m_', '-b_', $mediumimage);
Also backslashing along with other tries resulting from searches.
Warning: preg_replace(): No ending delimiter '-' found
I don't feel well right now...
I beseech anyone who is smarter than me e.g..... any one here.
Try use $biggerimage = str_replace("-b_", "-m_", $mediumimage);

Using preg_match to Find URL [duplicate]

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

preg_replace does not appear to be working am I missing delimiters [duplicate]

This question already has answers here:
parse youtube video id using preg_match [duplicate]
(10 answers)
Closed 8 years ago.
I have the following regex which works fine in a regex editor but when I pull it together in PHP i am getting and Unknown modifier '(' error come up.
preg_replace("(\[LINK\])(\S*)(\[\/LINK])", "<a>href=\'$2\'>$2</a>", $xtext);
This is my first question on SO so I hope I have given enough information. From my research I believe I am missing delimiters but tried ~ at the start and the end of the search pattern and still does not seem to work.
try this
preg_replace("/(\[LINK\])(\S*)(\[\/LINK])/", "<a>href=\'$2\'>$2</a>", $xtext);
Note the delimiters
Just try with:
$input = 'foo [LINK]http://google.com[/LINK] bar';
$output = preg_replace('/\[LINK\](.*?)\[\/LINK\]/', '$1', $input);
Output:
string 'foo http://google.com bar' (length=57)

Php Regex issue - "Unknown modifier R" [duplicate]

This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
I'm trying to extract some text in between a Request XML tag (between and tags) using this Regex:
(?<=(<Request>)).*(?=(</Request>))
RegexBuddy shows me that it's fine, but preg_match returns this error:
"Unknown modifier R". Placing a backslash before the "/" makes it so nothing returns. Does anyone know what the problem is?
Code:
$parsedQuery = fopen("c:\\query", "r") ;
$parsed ="".
while (!feof($parsedQuery)) {
$parsed .= fgets($parsedQuery) ;}
$reg = "#(?<=(<Request>)).*(?=(</Request>))#";
$match = array();
preg_match($reg, $parsed, $match);
print_r($match);
Edit: I now noticed that the file opens with an unidentified character (binary value is 3F) after the opening of each tag (the "<" character). I assume php's fgets implementation does this for security measures, could this be the problem, and is there any way to surpass it?
The problem is that in PHP (and in a lot of langugages) a regexp is something like /pattern/modifier where modifier could be, for example, g (multiple match). If you want to use / in your regexp you have to escape them with a \ :
/(?<=(<Request>)).*(?=(<\/Request>))/
See http://www.php.net/manual/en/pcre.pattern.php for more information about patterns in PHP.

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

Categories