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
php explode function for only first white space
提问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 dweeves
yes
是的
just call
打电话
explode(' ',$s, 2)
this will create an array with at most 2 elements
这将创建一个最多包含 2 个元素的数组
回答by SenorAmor
回答by deefour
回答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);