aeroswat Posted February 23, 2010 Share Posted February 23, 2010 How do you remove the last word in a string no matter the length and no matter what the word is? Link to comment https://forums.phpfreaks.com/topic/193141-remove-last-word/ Share on other sites More sharing options...
seventheyejosh Posted February 23, 2010 Share Posted February 23, 2010 like so? $test='this is a test string'; $ar=explode(' ',$test); $ar[count($ar)-1]=''; $new=implode(' ',$ar); echo $new; // gives 'this is a test' exit; Link to comment https://forums.phpfreaks.com/topic/193141-remove-last-word/#findComment-1017077 Share on other sites More sharing options...
aeroswat Posted February 23, 2010 Author Share Posted February 23, 2010 like so? $test='this is a test string'; $ar=explode(' ',$test); $ar[count($ar)-1]=''; $new=implode(' ',$ar); echo $new; // gives 'this is a test' exit; ya. I thought there might have been some functions that easily handled this other than breaking the string down into an array of space delimited strings and then piecing it back together. Thank you tho Link to comment https://forums.phpfreaks.com/topic/193141-remove-last-word/#findComment-1017079 Share on other sites More sharing options...
alpine Posted February 23, 2010 Share Posted February 23, 2010 another variant <?php $test = 'this is a test string'; echo implode(" ", array_slice(str_word_count($test, 1), 0, -1)); // this is a test ?> Link to comment https://forums.phpfreaks.com/topic/193141-remove-last-word/#findComment-1017083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.