php 删除某个字符后的字符串部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2588666/
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
Remove portion of a string after a certain character
提问by Belgin Fish
I'm just wondering how I could remove everything after a certain substring in PHP
我只是想知道如何在 PHP 中的某个子字符串之后删除所有内容
ex:
前任:
Posted On April 6th By Some Dude
I'd like to have it so that it removes all the text including, and after, the sub string "By"
我想要它,以便它删除所有文本,包括子字符串“By”和之后
Thanks
谢谢
回答by Austin Fitzpatrick
$variable = substr($variable, 0, strpos($variable, "By"));
In plain english: Give me the part of the string starting at the beginning and ending at the position where you first encounter the deliminator.
用简单的英语:给我字符串的一部分,从你第一次遇到分隔符的位置开始到结束。
回答by VolkerK
回答by squarecandy
How about using explode:
如何使用explode:
$input = 'Posted On April 6th By Some Dude';
$result = explode(' By',$input);
return $result[0];
Advantages:
好处:
- Very readable / comprehensible
- Returns the full string if the divider string (" By" in this example) is not present. (Won't return FALSE like some of the other answers.)
- Doesn't require any regex.
- "Regular expressions are like a particularly spicy hot sauce – to be used in moderation and with restraint only when appropriate."
- Regex is slower than explode(I assume preg_split is similar in speed to the other regex options suggested in other answers)
- Makes the second part of the string available too if you need it (
$result[1]would returnSome Dudein this example)
- 非常易读/易懂
- 如果分隔符字符串(在本例中为“By”)不存在,则返回完整字符串。(不会像其他一些答案一样返回 FALSE。)
- 不需要任何正则表达式。
- “正则表达式就像一种特别辣的辣酱——要适度使用,只有在适当的时候才克制。”
- 正则表达式比爆炸慢(我假设 preg_split 的速度与其他答案中建议的其他正则表达式选项相似)
- 如果需要,也可以使用字符串的第二部分(在本例
$result[1]中将返回Some Dude)
回答by JYelton
You could do:
你可以这样做:
$posted = preg_replace('/ By.*/', '', $posted);
echo $posted;
回答by HADI
One method would be:
一种方法是:
$str = 'Posted On April 6th By Some Dude';
echo strtok($str, 'By'); // Posted On April 6th
回答by Tim Yobson
Try this.
尝试这个。
function strip_after_string($str,$char)
{
$pos=strpos($str,$char);
if ($pos!==false)
{
//$char was found, so return everything up to it.
return substr($str,0,$pos);
}
else
{
//this will return the original string if $char is not found. if you wish to return a blank string when not found, just change $str to ''
return $str;
}
}
Usage:
用法:
<?php
//returns Apples
$clean_string= strip_after_string ("Apples, Oranges, Banannas",",");
?>
回答by jemfinch
Austin's answer works for your example case.
奥斯汀的答案适用于您的示例案例。
More generally, you would do well to look into the regular expression functionswhen the substring you're splitting on may differ between strings:
更一般地,当您拆分的子字符串在字符串之间可能不同时,您最好查看正则表达式函数:
$variable = preg_replace('/By.*/', '', $variable);
回答by Tim Cooper
$var = "Posted On April 6th By Some Dude";
$new_var = substr($var, 0, strpos($var, " By"));
回答by Salman A
回答by Ming-Tang
By using regular expression: $string = preg_replace('/\s+By.*$/', '', $string)
通过使用正则表达式: $string = preg_replace('/\s+By.*$/', '', $string)

![$_SERVER['HTTP_X_REQUESTED_WITH'] 是否存在于 PHP 中?](/res/img/loading.gif)