dyluck Posted May 23, 2010 Share Posted May 23, 2010 Hello Trying to figure this out, the function below works fine if the code doens't have to escape anything. Hopefully someone can help function getTextBetweenStrings($string,$front,$back) { $replacefrom = array("/",'^','$','.','[',']','|','(',')','?','*','+','{','}','\\','-'); $replaceto = array("\/",'\^','\$','\.','\[','\]','\|','\(','\)','\?','\*','\+','\{','\}','\\\\','\-'); $front = str_replace($replacefrom, $replaceto, $front); $back = str_replace($replacefrom, $replaceto, $back); $pattern = "/$front(.*?)$back/"; preg_match($pattern, $string, $matches); return $matches; } $value = '<h2>GE Profile Black 36 in. Electric Cooktop with Induction Elements</h2>'; echo implode("<br>",getTextBetweenStrings($value, '<h2>', '</h2>')); If you change the </h2> part to <Bh2> on both the $value and the function variable, it works fine. Thanks Link to comment https://forums.phpfreaks.com/topic/202682-simple-preg_match-function/ Share on other sites More sharing options...
dyluck Posted May 23, 2010 Author Share Posted May 23, 2010 Hello Trying to figure this out, the function below works fine if the code doens't have to escape anything. Hopefully someone can help function getTextBetweenStrings($string,$front,$back) { $replacefrom = array("/",'^','$','.','[',']','|','(',')','?','*','+','{','}','\\','-'); $replaceto = array("\/",'\^','\$','\.','\[','\]','\|','\(','\)','\?','\*','\+','\{','\}','\\\\','\-'); $front = str_replace($replacefrom, $replaceto, $front); $back = str_replace($replacefrom, $replaceto, $back); $pattern = "/$front(.*?)$back/"; preg_match($pattern, $string, $matches); return $matches; } $value = '<h2>GE Profile Black 36 in. Electric Cooktop with Induction Elements</h2>'; echo implode("<br>",getTextBetweenStrings($value, '<h2>', '</h2>')); If you change the </h2> part to <Bh2> on both the $value and the function variable, it works fine. Thanks Just had to remove the following and it worked: '\\', '\\\\', Link to comment https://forums.phpfreaks.com/topic/202682-simple-preg_match-function/#findComment-1062369 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.