PHP: How can I remove from String the last word?
check this
<?php
$str ='"Hi, I\'m Gian Marco" will be "Hi, I\'m Gian"';
$words = explode( " ", $str );
array_splice( $words, -1 );
echo implode( " ", $words );
?>
source : Remove last two words from a string
try with this :
$txt = "Hi, I'm Gian Marco";
$str= preg_replace('/\W\w+\s*(\W*)$/', '$1', $txt);
echo $str
out put
Hi, I'm Gian