Twitch Posted May 16, 2014 Share Posted May 16, 2014 Hello All, I have been wrestling with a regex for a couple of hours now and I finally had to give in and ask for help. The weird thing is that it works if there are no new lines in the text, it fails if there is a new line(s) present. The code: $matches = array(); $pattern = '~\[CUSTOM_TAG(.*?)\](.*?)\[/CUSTOM_TAG\]~'; preg_match_all($pattern, $html, $matches); if (!empty($matches[0])){ foreach($matches[0] as $code){ $parameter = preg_replace($pattern, '$1', $code); $content = preg_replace($pattern, '$2', $code);//get the content between the pattern }//foreach($matches[0] as $code){ }else{ echo 'Match failed'; }//if (!empty($matches[0])){ So with that code in mind, if the $html variable (the text to be processed) is: $html = '<h1>Hello, world!</h1><p style="color:#ff0000;">Some red text</p>'; A match is found. If the $html variable is: $html = '<h1>Hello, world!</h1> <p style="color:#ff0000;">Some red text</p>'; Match not found Hopefully I'm just missing something simple in my regex. Thanks in advance!Twitch Link to comment https://forums.phpfreaks.com/topic/288546-preg_match_all-fails-if-new-line-present/ Share on other sites More sharing options...
requinix Posted May 16, 2014 Share Posted May 16, 2014 By default . does not match newlines. Use the /s flag to make that happen. Link to comment https://forums.phpfreaks.com/topic/288546-preg_match_all-fails-if-new-line-present/#findComment-1479752 Share on other sites More sharing options...
Twitch Posted May 16, 2014 Author Share Posted May 16, 2014 Thanks for the quick reply equinox. I got it working. I was putting /s before the ~ when I just needed to put s after the ~ Thanks again! Link to comment https://forums.phpfreaks.com/topic/288546-preg_match_all-fails-if-new-line-present/#findComment-1479754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.