php php仅第一个空格的爆炸功能

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

php explode function for only first white space

php

提问by sandbox

I have a String Hello This is a String. I need to explode it in PHP only for the First White Space. How is that possible?

我有一个 String Hello This is a String。我只需要在 PHP 中为 First 爆炸它White Space。这怎么可能?

回答by diEcho

set limitparameter

设置限制参数

print_r(explode(' ', $str, 2));

Reference

Reference

回答by dweeves

yes

是的

just call

打电话

explode(' ',$s, 2)

this will create an array with at most 2 elements

这将创建一个最多包含 2 个元素的数组

回答by SenorAmor

Try

尝试

explode(' ', $your_string, 2)

explode(' ', $your_string, 2)

See more: explode()

查看更多:explode()

回答by deefour

An alternative to explode

爆炸的替代品

$str   = 'Hello This is a String';
$parts = preg_split('/(\s)/', $str, PREG_SPLIT_DELIM_CAPTURE);

回答by Scott M.

I'm assuming you mean a space character for this answer. instead of thinking in terms of explode()you should think in terms of "find the first character and split the string there."

我假设你的意思是这个答案的空格字符。而不是考虑explode()你应该考虑“找到第一个字符并在那里拆分字符串”。

$pos = strpos($inputString, ' ');
$part1 = substr($inputString, 0, $pos);
$part2 = substr($inputString, $pos+1);