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

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

Remove portion of a string after a certain character

phpstring

提问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

Ifyou're using PHP 5.3+ take a look at the $before_needle flag of strstr()

如果您使用的是 PHP 5.3+,请查看strstr()的 $before_needle 标志

$s = 'Posted On April 6th By Some Dude';
echo strstr($s, 'By', true);

回答by squarecandy

How about using explode:

如何使用explode

$input = 'Posted On April 6th By Some Dude';
$result = explode(' By',$input);
return $result[0];

Advantages:

好处:

回答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

You can use listand explodefunctions:

您可以使用listexplode功能:

list($result) = explode("By", "Posted On April 6th By Some Dude", 2);
// $result is "Posted On April 6th "

回答by Ming-Tang

By using regular expression: $string = preg_replace('/\s+By.*$/', '', $string)

通过使用正则表达式: $string = preg_replace('/\s+By.*$/', '', $string)