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:

<?php
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);
?>

Output

In My

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