php php字符串函数在最后一次出现字符之前获取子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5939412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:58:07  来源:igfitidea点击:

php string function to get substring before the last occurrence of a character

php

提问by ptamzz

$string = "Hello World Again".
echo strrchr($string , ' '); // Gets ' Again'

Now I want to get "Hello World" from the $string[The substring before the last occurrence of a space ' ' ]. How do I get it??

现在我想从$string[最后一次出现空格 ' ' 之前的子字符串] 中获取“Hello World” 。我怎么得到它??

采纳答案by sdleihssirhc

This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:

这是一种廉价的方法,但您可以拆分、弹出,然后加入以完成它:

$string = 'Hello World Again';
$string = explode(' ', $string);
array_pop($string);
$string = implode(' ', $string);

回答by meouw

$string = "Hello World Again";
echo substr($string, 0, strrpos( $string, ' ') ); //Hello World

If the character isn't found, nothing is echoed

如果未找到该字符,则不回显任何内容

回答by zaf

One (nice and chilled out) way:

一种(好的和冷静的)方式:

$string = "Hello World Again";
$t1=explode(' ',$string);
array_pop($t1);
$t2=implode(' ',$t1);
print_r($t2);

Other (more tricky) ways:

其他(更棘手的)方法:

$result = preg_replace('~\s+\S+$~', '', $string);

or

或者

$result = implode(" ", array_slice(str_word_count($string, 1), 0, -1));

回答by R T

strripos— Find the position of the last occurrence of a case-insensitive substring in a string

strripos— 查找不区分大小写的子字符串在字符串中最后一次出现的位置

 $string = "hello world again";
 echo substr($string, 0, strripos($string, ' ')); // Hello world

回答by Till

$myString = "Hello World Again";
echo substr($myString, 0, strrpos($myString, " "));

回答by DaveK

You can use a combination of strrpos, which gets the position of the last instance of a given string within a string, and substrto return the value.

您可以结合使用strrpos获取给定字符串在字符串中的最后一个实例的位置,并使用substr返回值。

回答by Arvind Bhardwaj

The correct implementation should be:

正确的实现应该是:

$string = "Hello World Again";
$pos = strrpos( $string, ' ');
if ($pos !== false) {
    echo substr($string, 0, $pos ); //Hello World
}

Otherwise if the character is not found it will print nothing. See following case:

否则,如果找不到该字符,它将不打印任何内容。见以下案例:

$string = "Hello World Again";
//prints nothing as : is not found and strrpos returns false.
echo substr($string, 0, strrpos( $string, ':') );

回答by Nick

You could just use:

你可以只使用:

$string = "Hello World Again";
echo preg_replace('# [^ ]*$', '', $string);

This will work regardless of whether the character occurs in the string or not. It will also work if the last character is a space.

无论字符是否出现在字符串中,这都将起作用。如果最后一个字符是空格,它也将起作用。

回答by Maciej P?usa

function cutTo($string, $symbol) {
    return substr($string, 0, strpos($string, $symbol));
}