Remove last word from string php

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

Remove last word from string php

4

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

answered 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

Remove last word from string php

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.'';
}

Remove last word from string php

Khalid

4,6125 gold badges26 silver badges50 bronze badges

answered Apr 3, 2015 at 8:49

Remove last word from string php

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

Remove last word from string php

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 Gian

answered May 17, 2021 at 14:08

Remove last word from string php

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

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 .