PHP – Get the First Two Words of a String

This following PHP example helps you to get the first two words of a string.

Code Example:

function first_two_words($val) {
	$val2 = substr($val, strpos($val, ' ')+1);
	$val = substr($val, 0, strlen($val)-strlen($val2)+strpos($val2, ' '));
	return $val;

$str = 'In My Cart : 12 14 items';
echo first_two_words($str);


In My

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail