How can I remove, with PHP, the last word from a String?
For example the string "Hi, I'm Gian Marco" would become "Hi, I'm Gian".
asked Apr 3, 2015 at 8:01
4
try with this :
$txt = "Hi, I'm Gian Marco"; $str= preg_replace('/\W\w+\s*(\W*)$/', '$1', $txt); echo $strout put
Hi, I'm Giananswered Apr 3, 2015 at 8:18
Ahmed ZianiAhmed Ziani
1,1663 gold badges13 silver badges26 bronze badges
2
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
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
$text = "Hi, I'm Gian Marco"; $last_space_position = strrpos($text, ' '); $text = substr($text, 0, $last_space_position); echo $text;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:
foreach ($this->id as $key => $id) { $sql.=' id =' . $id . ' OR '; }A better solution:
$sql_chain = chop($sql_chain," OR ");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 :
$str="Hi, I'm Gian Marco"; $split=explode("",$str); $split_rem=array_pop($split); foreach ($split as $k=>$v) { echo $v.''; }
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:
$input = "Hi, I'm Gian Marco"; $output = substr($input, 0, strrpos($input, " ")); echo $output;answered Aug 17, 2020 at 17:05
SpesmSpesm
711 silver badge5 bronze badges
Explode the str with explode(delimiter, string) into an array $words, take the first word put it into $res, and iterate over words the count of $words times minus one which is exclude the last word.
$str = "Hi, I'm Gian Marco"; $words = explode(" ",$str); $res = $words[0]; for ($i=1; $i < count($words) - 1; $i++) { $res .= ' '.$words[$i]; // concatenation with a space. } echo $res; // Hi, I'm Giananswered 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.
function leftStrTrim($sentence, $word){ $wordLength = strlen($word); $wordStart = strpos($sentence, $word); if($wordStart!==false && $wordStart==0){ return substr($sentence, $wordLength); } return $sentence; }Find the last occurrence of a given word and if it is the last word, return the sentence without that last word.
function righStrTrim($sentence, $word){ $wordLength = strlen($word); $wordStart = strrpos($sentence, $word); if($wordStart!==false && $wordStart == strlen($sentence)-$wordLength){ return substr($sentence, 0, $wordLength*-1); } return $sentence; }answered Jun 1, 2021 at 6:50
FrankeyFrankey
3,64027 silver badges25 bronze badges