Jump to content

preg_match with variables. how escape forward slash in variable


Recommended Posts

Hi,

I need some help. below is an example,

 

$str = "<title>some text goes here</title> <title>some other text goes here</title>";

$start = "<title>";

$end = "</title>";

preg_match_all('/'.$start.'(.*)'.$end.'/U', $str, $output);

 

I got this error "Warning: preg_match() [function.preg-match]: Unknown modifier 'p'."

Above is just an example, and the main problem is if in any of my variables, I have a forward slash, I will get an error. How do you solve this problem if theres a slash in your variable which is then goes into preg_match. If I was to escape the forward slash in my variable before preg_match (ie. $end = "<\/title>") , I won't get any results back.

 

Thanks.

<?php
function get_string_between($string, $start, $end){ 
    $string = " ".$string; 
    $ini = strpos($string,$start); 
    if ($ini == 0) return ""; 
    $ini += strlen($start); 
    $len = strpos($string,$end,$ini) - $ini; 
    return substr($string,$ini,$len); 
} 

$fullstring = "<title>test</title>"; 
$parsed = get_string_between($fullstring, "<title>", "</title>"); 

echo $parsed;
?>

 

try that

Hi,

 

thank you for your reply. This is a work-around way which won't help me because I tried to grab data from various external sites. Some of them will have forward slash. I need a way to escape them. To my understanding, you wrote your own regex function but then in the end, you entered $parsed = get_string_between($fullstring, "<title>", "</title>"); which if replaced $start and $end string with <title> and </title>, it won't work. I have to use variable in preg_match, I can't pass in string directly in my problem. All I'm asking is

preg_match_all('/'.$start.'(.*)'.$end.'/U', $str, $output);

what do you do if $end or $start contains a "/", how do you escape them? They're are part of regex so you can't just escape them before hand then plugin because then it won't match with what's actually on the external sources.

 

Thanks.

I know you got it working, but for future reference, you might look at the preg_quote() function.  Also, your regular expression does not have to be delimited by '/' you can actually use any character.  it is best to use one that is not going to be in the expression.  So

preg_match('/php/i', $string);

is equivalent to

preg_match('-php-i', $string);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.