php 如何删除字符串中第一个特定字符之前的所有内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5329866/
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 21:01:53  来源:igfitidea点击:

How to remove everything before the first specific character in a string?

phpstringreplace

提问by Andrej

My variables look like this:

我的变量如下所示:

AAAAAAA, BB CCCCCCCC

AAAAAAA, BB CCCCCCCC

AAAA,BBBBBB CCCCCC

AAAA,BBBBBB CCCCCC

I would like to remove everything before the ",",

我想删除“ ,”之前的所有内容,

so the results should look like:

所以结果应该是这样的:

BB CCCCCCCC

BB CCCCCC

BBBBBB CCCCCC

BBBBBB中交

I have worked out this to remove everything AFTER the ",":

我已经解决了这个问题,以删除“ ,”之后的所有内容:

list($xxx) = explode(',', $yyyyy);

unfortunately I dont know how to get it to work to remove everything BEFORE the ",".

不幸的是,我不知道如何让它工作以删除“ ,”之前的所有内容。

回答by Andrew Moore

Since this is a simple string manipulation, you can use the following to remove all characters before the first comma:

由于这是一个简单的字符串操作,您可以使用以下命令删除第一个逗号之前的所有字符:

$string = preg_replace('/^[^,]*,\s*/', '', $input);

preg_replace()allows you to replace parts of a string based on a regular expression. Let's take a look at the regular expression.

preg_replace()允许您根据正则表达式替换部分字符串。我们来看看正则表达式。

  • / is the start delimiter
    • ^ is the "start of string" anchor
    • [^,] every character that isn't a comma (^ negates the class here)
      • * repeated zero or more times
    • , regular comma
    • \s any whitespace character
      • * repeated zero or more times
  • / end delimiter
  • / is the start delimiter
    • ^ is the "start of string" anchor
    • [^,] every character that isn't a comma (^ negates the class here)
      • * repeated zero or more times
    • , regular comma
    • \s any whitespace character
      • * repeated zero or more times
  • / end delimiter

回答by Tim Cooper

I wouldn't recommend using explode, as it causes more issues if there is more than one comma.

我不建议使用爆炸,因为如果有多个逗号,它会导致更多问题。

// removes everything before the first ,
$new_str = substr($str, ($pos = strpos($str, ',')) !== false ? $pos + 1 : 0);

Edit:

编辑:

if(($pos = strpos($str, ',')) !== false)
{
   $new_str = substr($str, $pos + 1);
}
else
{
   $new_str = get_last_word($str);
}

回答by nickf

list(,$xxx) = explode(',', $yyyyy, 2);

回答by Lawrence Cherone

try this it gets the last stuff after the , if no , is present it will check from the last space, i wrapped it in a function to make it easy:

试试这个,它获取 之后的最后一个东西,如果没有,它会从最后一个空间检查,我把它包装在一个函数中以使其容易:

<?php 
$value='AAAA BBBBBB CCCCCC';
function checkstr($value){
    if(strpos($value,',')==FALSE){
        return trim(substr(strrchr($value, ' '), 1 ));  
    }else{
        return trim(substr($value, strpos($value,',')),',');
    }
}

echo checkstr($value);
?>

回答by Naftali aka Neal

you can do:

你可以做:

$arr = explode(',', $yyyyy);
unset($arr[0]);
echo implode($arr);

回答by TarranJones

Regex is generally expensive and i wouldn't recommend it for something as simple as this. Using explode and limiting it to 2 will probably result in the same execution time as using str_pos but you wouldn't have to do anything else to generate the required string as its stored in the second index.

正则表达式通常很昂贵,我不会推荐它用于像这样简单的事情。使用爆炸并将其限制为 2 可能会导致与使用 str_pos 相同的执行时间,但您无需执行任何其他操作即可生成所需的字符串,因为它存储在第二个索引中。

 //simple answer 
 $str = explode(',', $yyyyy,2)[1]; 

OR

或者

//better 

$arr = explode(',', $yyyyy,2);
$str = isset($arr[1]) ? $arr[1] : '';