Monday, April 26, 2010

PHP Retrieve string

Retrieve string from HTML or XML or any text.....

$string = "This is My text test Your text";
echo retrieve($string,"",""); // output: My text
echo retrieve($string,"","",2); // output: Your text


function retrieve($data, $from, $till, $freq){
if ($freq==0){$freq=1;}
//if (strpos($data, $from)==FALSE) return $data;
//if (strpos($data, $till)==FALSE) return $data;
while($freq){

$temp = substr($data, strpos($data,$from),(strlen($data)-strpos($data,$from)));
$freq--;
$data=substr($temp,strlen($from));
}
$temp = substr($temp,strlen($from),strpos($temp,$till)-strlen($from));
return $temp;
}