如何删除字符串中第一个特定字符之前的所有内容? [英] How to remove everything before the first specific character in a string?
本文介绍了如何删除字符串中第一个特定字符之前的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的变量如下:
AAAAAAA,BB CCCCCCCC
AAAA,BBBBBB CCCCCC
我想删除",
"之前的所有内容,
I would like to remove everything before the ",
",
因此结果应如下所示:
BB CCCCCCCC
BBBBBB CCCCCC
我已经解决了此问题,以便删除",
"之后的所有内容:
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 ",
".
推荐答案
我不建议使用explode,因为如果有多个逗号,则会引起更多问题.
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);
if(($pos = strpos($str, ',')) !== false)
{
$new_str = substr($str, $pos + 1);
}
else
{
$new_str = get_last_word($str);
}
这篇关于如何删除字符串中第一个特定字符之前的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文