简体   繁体   English

PHP:如何从字符串中删除最后一个单词?

[英]PHP: How can I remove from String the last word?

How can I remove, with PHP, the last word from a String?如何使用 PHP 从字符串中删除最后一个单词?

For example the string "Hi, I'm Gian Marco" would become "Hi, I'm Gian" .例如,字符串"Hi, I'm Gian Marco"将变成"Hi, I'm Gian"

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

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 source : 从字符串中删除最后两个单词

You can do it with regular expression.你可以用正则表达式来做到这一点。 (see answer of Ahmed Ziani. ) (见Ahmed Ziani 的回答

However, in PHP you can also do it using some inbuilt function.但是,在 PHP 中,您也可以使用一些内置函数来完成。 see the code below看下面的代码

$text = "Hi, I'm Gian Marco";
$last_space_position = strrpos($text, ' ');
$text = substr($text, 0, $last_space_position);
echo $text;

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:如果您确实知道它,例如在为这样的查询循环 concat 字符串时:

        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.请注意,带有正则表达式的 preg_replace 对于长字符串非常慢。 Chop is 100 times faster in such case and perf gain can be substantial.在这种情况下,Chop 的速度要快 100 倍,而且性能增益可能很大。

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

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;

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.使用explode(delimiter, string)str分解为数组$words ,将第一个单词放入$res ,然后迭代单词的次数为$words减去一个,即排除最后一个单词。

$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

In search for ltrim and rtrim alternatives (to trim a whole first/last word) I came across posts like these.在寻找 ltrim 和 rtrim 替代品(修剪整个第一个/最后一个词)时,我遇到了这样的帖子。 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;    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM