Jump to content

preg_match_all fails if new line present


Go to solution Solved by requinix,

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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