我正在尝试拆分字符串,如下所示
<?php
$str = "Q:1) What is PHP?Opensource,cms,framework,webservice,opensource
Q:2) What is Laravel?Opensource,cms,framework,webservice,framework
Q:3) What is Wordpress?Opensource,cms,framework,webservice,framwork
Q:4) What is Shopify?Opensource,cms,framework,webservice,framwork
Q:5) What is Mangento?Opensource,cms,framework,webservice,framwork";
$ex = explode("Q:",$str);
echo $ex[0];
它什么也没显示
答案 0 :(得分:4)
第0个索引将为空,因为Q:
位于起始位置。所以从索引1开始
您的结果将是:
Array
(
[0] =>
[1] => 1) What is PHP?Opensource,cms,framework,webservice,opensource
[2] => 2) What is Laravel?Opensource,cms,framework,webservice,framework
[3] => 3) What is Wordpress?Opensource,cms,framework,webservice,framwork
[4] => 4) What is Shopify?Opensource,cms,framework,webservice,framwork
[5] => 5) What is Mangento?Opensource,cms,framework,webservice,framwork
)
可能的解决方法:
您可以通过应用array_shift
:
array_shift($ex);
这会将元素从爆炸数组的开头移开。然后你的数组将成为:
Array
(
[0] => 1) What is PHP?Opensource,cms,framework,webservice,opensource
[1] => 2) What is Laravel?Opensource,cms,framework,webservice,framework
[2] => 3) What is Wordpress?Opensource,cms,framework,webservice,framwork
[3] => 4) What is Shopify?Opensource,cms,framework,webservice,framwork
[4] => 5) What is Mangento?Opensource,cms,framework,webservice,framwork
)
答案 1 :(得分:2)
那是因为第一次出现&#34; Q:&#34;在字符串的最开头,所以$ ex中的第一项是一个空字符串。尝试输出第二个项目$echo $ex[1];
,它应该给你
"1) What is PHP?Opensource,cms,framework,webservice,opensource"