becu Posted March 20, 2010 Share Posted March 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/ Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 Please tell me what your trying to accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/#findComment-1029016 Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/#findComment-1029019 Share on other sites More sharing options...
becu Posted March 20, 2010 Author Share Posted March 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/#findComment-1029159 Share on other sites More sharing options...
sasa Posted March 20, 2010 Share Posted March 20, 2010 $end='<\/title>'; Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/#findComment-1029182 Share on other sites More sharing options...
becu Posted March 21, 2010 Author Share Posted March 21, 2010 Hello, turn out solution of Ruzzas works. Thank you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/#findComment-1029372 Share on other sites More sharing options...
Ruzzas Posted March 21, 2010 Share Posted March 21, 2010 Your welcome. Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/#findComment-1029384 Share on other sites More sharing options...
DavidAM Posted March 21, 2010 Share Posted March 21, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/195900-preg_match-with-variables-how-escape-forward-slash-in-variable/#findComment-1029388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.