Page 1 of 1

preg_match_all

Posted: Mon Feb 08, 2010 1:06 am
by cali_dotcom
Hi all, i am using preg_match_all to the ids and contents within a custom html tag, it all works fine if the contents within the tags are all in one line, but if they go over multiple lines, it doesn't match it.

here is the code and a sample of the tags:

Code: Select all

 
    function getTranslationID($string){
    
    $pattern = "/<translate RID=\"(.*?)\">(.*?)<\/translate>/";
    preg_match_all($pattern,$string,$matches);
    $matched['id'] = $matches[1];
    $matched['contents'] = $matches[2];
    if(!empty($matches[1])){
            return $matched;
    } else {
            return NULL;
        }
}
// a sample of html in php to match
<tranlslate RID="002"> contents..... ..... ..... ... whwhhw.....
;;;;;......................
................contents</translate>
 
//it works fine with:
<translate RID="002">contents  ... ..... ..... ..... ...</translate>
 
 
can anyone tell me what im doing wrong? thanks......

Re: preg_match_all

Posted: Mon Feb 08, 2010 9:44 am
by infolock
I am not actually looking too much into this, but maybe try using nl2br on your string.

ie:

Code: Select all

 
preg_match_all($pattern, nl2br($string),$matches);
 
Perhaps that will fix it? if you want your newlines back, just use br2nl, there is a nice user-contributed function found on nl2br's page that you can add that will do this for you:

Code: Select all

 
<?php
function br2nl($string){
  $return=eregi_replace('<br[[:space:]]*/?'.
    '[[:space:]]*>',chr(13).chr(10),$string);
  return $return;
}
?> 
 
Again, I just assume this will work. haven't tried it. good luck

Re: preg_match_all

Posted: Mon Feb 08, 2010 10:36 am
by AbraCadaver
The . doesn't match newlines by default. Use the s modifier:

Code: Select all

$pattern = "/<translate RID=\"(.*?)\">(.*?)<\/translate>/s";