Jump to content

preg_match_all fails if new line present


Twitch

Recommended Posts

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

Archived

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

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