Anti-Moronic Posted August 14, 2009 Share Posted August 14, 2009 What I want to do is split a string by the string position, like so: string position: 4 $str = "123|321|123"; what I want to output is 2 strings into an array: array('123|', '321|123'); Any help is greatly appreciated. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/ Share on other sites More sharing options...
RichardRotterdam Posted August 14, 2009 Share Posted August 14, 2009 try explode edit: Darn I mean substr you could use explode and add the last two entries together Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/#findComment-898151 Share on other sites More sharing options...
Anti-Moronic Posted August 14, 2009 Author Share Posted August 14, 2009 Can't use explode on this. That would remove the | and split the string into 3: array('123', '321', '123'); Thanks anyway. Maybe there is a way to do it with explode, but I can't reply on a character, I need to rely on an actual position within the array. Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/#findComment-898152 Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 substr() will do what you want Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/#findComment-898153 Share on other sites More sharing options...
Anti-Moronic Posted August 14, 2009 Author Share Posted August 14, 2009 I was hoping substr wasn't the only way. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/#findComment-898154 Share on other sites More sharing options...
RichardRotterdam Posted August 14, 2009 Share Posted August 14, 2009 I was hoping substr wasn't the only way. Thanks. It isn't you can also use a regular expression with one of the preg_ functions edit darn beer Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/#findComment-898159 Share on other sites More sharing options...
.josh Posted August 14, 2009 Share Posted August 14, 2009 $pos = 4; $str = "123|321|123"; $str = preg_split('~(.{'.$pos.'})~s',$str,2,PREG_SPLIT_DELIM_CAPTURE); echo "<pre>"; print_r($str); output: Array ( [0] => [1] => 123| [2] => 321|123 ) Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/#findComment-898197 Share on other sites More sharing options...
Anti-Moronic Posted August 14, 2009 Author Share Posted August 14, 2009 Wow, thank you very much CV - very much appreciated. Now I can use preg_split! thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/170265-solved-split-string-into-array-by-string-position-not-delimiter/#findComment-898245 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.