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? Quote Link to comment 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; Quote Link to comment 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 Quote Link to comment 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 ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.