pdug175 Posted April 7, 2007 Share Posted April 7, 2007 Hi, could someone please explain to my why using the code below generates a blank item in $array[0] please. The 'x' from xbox360 is in $array[1]? Thank you. $str="xbox360"' $array= preg_split('//', $str, -1); echo $array[0]; Link to comment https://forums.phpfreaks.com/topic/45957-preg_split-first-item-in-array-is-blank/ Share on other sites More sharing options...
fert Posted April 7, 2007 Share Posted April 7, 2007 try this $str="xbox360"' $array= preg_split('//', $str, -1); print_r($array); Link to comment https://forums.phpfreaks.com/topic/45957-preg_split-first-item-in-array-is-blank/#findComment-223249 Share on other sites More sharing options...
pdug175 Posted April 7, 2007 Author Share Posted April 7, 2007 Hi fert, thanks for the reply. I've not posted all my code because I'm doing some further manipulation with the string. I'm particularly interested why the first item isn't being assigned to $array[0], is there something stupid I'm doing with that code? Link to comment https://forums.phpfreaks.com/topic/45957-preg_split-first-item-in-array-is-blank/#findComment-223257 Share on other sites More sharing options...
akitchin Posted April 7, 2007 Share Posted April 7, 2007 i have a feeling that's just how the code works - given that it "splits" on a regex, it will likely return the portion both before and after the match; that includes before the first position and after, which results in the empty first return. try using: <?php $array= preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); ?> this will tell it to not bother returning any empty matches. Link to comment https://forums.phpfreaks.com/topic/45957-preg_split-first-item-in-array-is-blank/#findComment-223265 Share on other sites More sharing options...
pdug175 Posted April 7, 2007 Author Share Posted April 7, 2007 ahh, thank you. I was just curious more than anything, your taught that '(0) is the first item in an array - seemed strange that it was blank Link to comment https://forums.phpfreaks.com/topic/45957-preg_split-first-item-in-array-is-blank/#findComment-223276 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.