- array_splice: documentation ( source)
- implode: documentation ( source)
- explode: documentation ( source)
<?php
$string = 'this is a returned string';
$words = explode(" ", $string );
$last_two_word = implode(' ',array_splice($words, -2 ));
$except_last_two = implode(' ', $words);
$expected = '<p>'.$except_last_two.' <span class="someclass">'.$last_two_word.'</span></p>';
echo $expected;
?>