Thursday, January 29, 2015

Get content between two special character from a string in php

<?php
$string='[countrycontent countryname="GB"] london [/countrycontent]

[countrycontent countryname="IN"] INDIA [/countrycontent] [countrycontent countryname="IN"] INDIA [/countrycontent]';
preg_match_all('/\](.*?)\[/',$string, $matches);

// array [1] is the stripped matches... array[0] would reurn with the []'s :)
$matches1 = $matches[1];
$contentA=array();
// cycle through the matches and add them to the searched for string
foreach ($matches1 as $match1){
 
   $contentA[]=$match1;
}



preg_match_all('/\"(.*?)\"/',$string, $matches);

// array [1] is the stripped matches... array[0] would reurn with the []'s :)
$matches2 = $matches[1];
 $countryA=array();
// cycle through the matches and add them to the searched for string
foreach ($matches2 as $match2){

   $countryA[]=$match2;
}

$result = array_combine($countryA, $contentA);

foreach($result as $key=>$v)
{
if($key=="GB")
{
echo $v;
}

}




?>

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts