Jump to content

Archived

This topic is now archived and is closed to further replies.

becu

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.

Share this post


Link to post
Share on other sites

<?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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
$end='<\/title>';

Share this post


Link to post
Share on other sites

Hello,

 

turn out solution of Ruzzas works. Thank you so much!!!

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

×
×
  • 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.