How can I remove, with PHP, the last word from a String? Show For example the string
asked Apr 3, 2015 at 8:01
4 try with this :
out put
answered Apr 3, 2015 at 8:18
Ahmed ZianiAhmed Ziani 1,1663 gold badges13 silver badges26 bronze badges 2 check this
source : Remove last two words from a string
answered Apr 3, 2015 at 8:28
1 You can do it with regular expression. (see answer of Ahmed Ziani.) However, in PHP you can also do it using some inbuilt function. see the code below
answered Mar 9, 2016 at 7:27
Nishad UpNishad Up 3,1971 gold badge26 silver badges30 bronze badges 1 The current solution is ok if you do not know the last word and the string length is short. In case you do know it, for instance when looping a concat string for a query like this:
A better solution:
Be aware that preg_replace with a regex is VERY slow with long strings. Chop is 100 times faster in such case and perf gain can be substantial. answered Sep 26, 2015 at 18:01
cpugouroucpugourou 7777 silver badges11 bronze badges This code may help you :
Khalid 4,6125 gold badges26 silver badges50 bronze badges answered Apr 3, 2015 at 8:49
lakshmanlakshman 6464 silver badges18 bronze badges 1 The regular exprerssion can cause issues when the string has special characters. Why not simply go with this:
answered Aug 17, 2020 at 17:05
SpesmSpesm 711 silver badge5 bronze badges Explode the
answered May 17, 2021 at 14:08
TAHER El MehdiTAHER El Mehdi 3,9632 gold badges5 silver badges21 bronze badges In search for ltrim and rtrim alternatives (to trim a whole first/last word) I came across posts like these. Just want to share the functions I wrote when any other is landing here like I did. Find the first occurrence of a given word and if it is the first word, return the sentence without that first word.
Find the last occurrence of a given word and if it is the last word, return the sentence without that last word.
answered Jun 1, 2021 at 6:50
FrankeyFrankey 3,64027 silver badges25 bronze badges How to take last word from string in php?$string = 'Retrieving the last word of a string using PHP. '; $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result $last_word = substr($string, $last_word_start); // $last_word = PHP.
Which script can you use in a regular expression in PHP to remove the last word from a string?PHP Code: <? php $str1 = 'The quick brown fox'; echo preg_replace('/\W\w+\s*(\W*)$/', '$1', $str1).
How do I remove the last two words from a string in Java?How to Remove Last Character from String in Java. Using StringBuffer.deleteCahrAt() Class.. Using String.substring() Method.. Using StringUtils.chop() Method.. Using Regular Expression.. How do you check if a string contains another string in PHP?Answer: Use the PHP strpos() Function
You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .
|