|
Posted by Justin Koivisto on 05/05/06 22:55
leonardo.calado@gmail.com wrote:
> Hi, I have the following text from database:
>
> {geo:countries:fr|br|us|es}
> Vivamus orci. Nunc eget risus adipiscing urna interdum tincidunt.
> Aenean nec felis condimentum nisi rhoncus tristique. Cras fringilla
> posuere nulla. Fusce eu justo.
> {/geo}
>
> {geo:countries:all}
> Proin ac sem. Praesent faucibus libero nec quam. Cras id neque. Mauris
> euismod eleifend elit. Mauris id sem. Donec odio.
> {/geo}
>
> {geo:countries:uk|jp|ch}
> Vestibulum viverra metus vel diam. Maecenas eu purus. Etiam fringilla
> eleifend libero.
> {/geo}
>
> I need split this information in array and show only content from
> listed country or all countries.
>
> I'm try using split()"http://www.php.net/split" function with regex,
> but I'm not success, I try, try and try... and nothing, I try using a
> pattern from regex.
>
> Anybody help me?
>
> Thanks for all.
>
<?php
$text= '{geo:countries:fr|br|us|es}
Vivamus orci. Nunc eget risus adipiscing urna interdum tincidunt.
Aenean nec felis condimentum nisi rhoncus tristique. Cras fringilla
posuere nulla. Fusce eu justo.
{/geo}
{geo:countries:all}
Proin ac sem. Praesent faucibus libero nec quam. Cras id neque. Mauris
euismod eleifend elit. Mauris id sem. Donec odio.
{/geo}
{geo:countries:uk|jp|ch}
Vestibulum viverra metus vel diam. Maecenas eu purus. Etiam fringilla
eleifend libero.
{/geo}
';
$geopat='`{geo\:countries\:([^}]+)}(.*)(?={/geo}){/geo}`isU';
$texts=array();
if(preg_match_all($geopat,$text,$m)){
$numMatches=count($m[0]);
for($i=0;$i<$numMatches;$i++){
$countries=explode('|',$m[1][$i]);
foreach($countries as $k){
if(!isset($texts[$k])) $texts[$k]=array();
$texts[$k][]=$m[2][$i];
}
}
}
print_r($texts);
?>
HTH
--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
[Back to original message]
|