I am trying to split string like below
<?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];
It displays nothing
2
Answers
The 0th index will be empty since th
Q:
is at the starting location. So start with index 1 onwardsYour result would be:
Possible workaround:
You can over come this by applying
array_shift
:This will shift an element off the beginning of your exploded array. Then your array will become:
That is because the first occurrence of “Q:” is at the very beginning of the string, so the first item in $ex is an empty string. try outputting the second item instead
$echo $ex[1];
, it should give you"1) What is PHP?Opensource,cms,framework,webservice,opensource"