preg_match_all

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cali_dotcom
Forum Commoner
Posts: 49
Joined: Fri Aug 22, 2008 7:28 pm
Location: Rancho Cucamonga, CA

preg_match_all

Post 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......
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: preg_match_all

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: preg_match_all

Post by AbraCadaver »

The . doesn't match newlines by default. Use the s modifier:

Code: Select all

$pattern = "/<translate RID=\"(.*?)\">(.*?)<\/translate>/s";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply