Friday, August 5, 2011

Find string between two strings - PHP

This is a little function to find a string between two specified pieces of strings. This could be used to find any string between two strings.

function get_string($string, $start, $end){
 $string = " ".$string;
 $pos = strpos($string,$start);
 if ($pos == 0) return "";
 $pos += strlen($start);
 $len = strpos($string,$end,$pos) - $pos;
 return substr($string,$pos,$len);
}

$fullstring = "Find string between two strings in PHP";
$parsed = get_string($fullstring, "between ", " in PHP");

echo $parsed; 
(result = two strings)

0 comments:

Post a Comment