在 PHP 中删除从字符第一次出现到字符串末尾的所有内容

发布于 2024-07-26 15:04:43 字数 189 浏览 2 评论 0原文

我想删除 PHP 中字符串的第一个逗号中的所有内容(包括逗号)。

例如,

$print = "50 days,7 hours";

应该变成

50 days

I want to remove everything (including the comma) from the first comma of a string in PHP.

For example,

$print = "50 days,7 hours";

should become

50 days

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

焚却相思 2024-08-02 15:04:44

您可以使用正则表达式,但如果它始终是与逗号的单个配对,我会这样做:


$printArray = explode(",", $print);
$print = $printArray[0];

You could use a regular expression, but if it's always going to be a single pairing with a comma, I'd just do this:


$printArray = explode(",", $print);
$print = $printArray[0];
幸福%小乖 2024-08-02 15:04:44

您还可以使用 current 函数:

$firstpart = current(explode(',', $print)); // will return current item in array, by default first

还有该系列的其他函数:

$nextpart = next(explode(',', $print)); // will return next item in array

$lastpart = end(explode(',', $print)); // will return last item in array

You can also use current function:

$firstpart = current(explode(',', $print)); // will return current item in array, by default first

Also other functions from this family:

$nextpart = next(explode(',', $print)); // will return next item in array

$lastpart = end(explode(',', $print)); // will return last item in array
爱,才寂寞 2024-08-02 15:04:44
$string="50 days,7 hours";  
$s = preg_split("/,/",$string);
print $s[0];
$string="50 days,7 hours";  
$s = preg_split("/,/",$string);
print $s[0];
无边思念无边月 2024-08-02 15:04:44

如果您要使用 strstr(),那么您需要 100% 确定字符串中存在逗号,或者准备好处理 false 返回值。 将其第三个参数设置为 true 以访问第一个出现的逗号之前的所有字符。 如果没有逗号,则返回值为false

preg_replace() 可能是效率最低的,但它是单个函数调用,如果找不到逗号,我将使用的模式不会改变字符串。 如果您的输入字符串中可能包含换行符,请使用 s 模式修饰符来允许点 (.) 也匹配这些换行符。

strtok() 是一个简洁的工具,但我发现它的名字比其他函数缺乏表达力/更神秘(也许这是我个人对 strstr() 的偏见 则如果输入字符串没有长度,这可能会令未来的读者感到困惑或减慢速度。

>。如果您要使用 explode(), ,那么不要要求 php 执行超过 1 次爆炸。这个函数的好处是,如果逗号不存在,那么它将返回整个字符串,我不喜欢使用 explode()<。 /code> 因为它生成一个数组,从中访问第一个元素 - 我不想生成超出我需要的数据,

因为否定字符对于此任务来说有点太笨拙了 。需要使用类以及空合并运算符如果分隔符是空格,则可以使用 %s,但空合并运算符仍然是必要的,因为 sscanf() 不会进行零长度匹配。

代码:(演示) (如果没有逗号则演示) (如果字符串为空则演示)

"50 天,7 小时""50 天"""
strstr($print, ',', true)"50 天"falsefalse
preg_replace('/,.*/', '', $print)"50 天""50 天"""
strtok($print, ",")"50 天""50 天"false
explode( ',', $print, 2)[0]"50 天""50 天"""
sscanf($print, '%[^,]')[0] ?? $print"50 天""50 天"""

If you are going to use strstr() then you need to be 100% sure that a comma will exist in the string or be prepared to handle a false return value. Set its 3rd parameter as true to access all characters before the first occurring comma. If there is no comma, then the return value will be false.

preg_replace() will likely be the least efficient, but it is a single function call and the pattern that I'll use will not mutate the string if a comma is not found. If your input string might have newline characters in it, use the s pattern modifier to allow the dot (.) to match these as well.

strtok() is a concise tool to use, but I find its name to be less expressive / more cryptic than other functions (maybe this is my own personal bias toward strstr(). This may confuse or slow down future readers of your code. This function will return false if the input string has no length.

If you are going to use explode(), then don't ask php to perform more than 1 explosion. The good thing about this function is that if the comma doesn't exist, then it will return the whole string. I prefer not to use explode() because it generates an array from which the first element is accessed -- I prefer to not generate more data than I need.

sscanf() is a little too clumsy for this task since a negated character class needs to be used as well as the null coalescing operator. If the delimiter was a space, then %s could be used, but the null coalescing operator would still be necessary because sscanf() will not make zero-length matches.

Code: (Demo) (Demo if no comma) (Demo if string empty)

"50 days,7 hours""50 days"""
strstr($print, ',', true)"50 days"falsefalse
preg_replace('/,.*/', '', $print)"50 days""50 days"""
strtok($print, ",")"50 days""50 days"false
explode(',', $print, 2)[0]"50 days""50 days"""
sscanf($print, '%[^,]')[0] ?? $print"50 days""50 days"""
心不设防 2024-08-02 15:04:43

这是一种方法:

$print = preg_replace('/^([^,]*).*$/', '$1', $print);

另一种:

list($print) = explode(',', $print);

或者:

$print = explode(',', $print)[0];

Here's one way:

$print = preg_replace('/^([^,]*).*$/', '$1', $print);

Another:

list($print) = explode(',', $print);

Or:

$print = explode(',', $print)[0];
独夜无伴 2024-08-02 15:04:43

这应该适合你:

$r = (strstr($print, ',') ? substr($print, 0, strpos($print, ',')) : $print);
# $r contains everything before the comma, and the entire string if no comma is present

This should work for you:

$r = (strstr($print, ',') ? substr($print, 0, strpos($print, ',')) : $print);
# $r contains everything before the comma, and the entire string if no comma is present
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文