play_ Posted May 22, 2007 Share Posted May 22, 2007 trying to explode a word at every character. i couldve sworn a long time ago i've done explode('', $word). but now it says the delimiter is invalid. So, how do i accomplish this? on a side note, #php on freenode is always full and sends me to the overflow channel. does anyone know of another good channel? Link to comment https://forums.phpfreaks.com/topic/52438-explode/ Share on other sites More sharing options...
jitesh Posted May 22, 2007 Share Posted May 22, 2007 <?php $str = "test"; $str_arr = array(); for($i=0;$i<strlen($str);$i++){ $str_arr[] = substr($str,$i,1); } ?> Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258758 Share on other sites More sharing options...
play_ Posted May 22, 2007 Author Share Posted May 22, 2007 Strange. i couuld've sworn it worked before. but thanks jitesh Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258759 Share on other sites More sharing options...
play_ Posted May 22, 2007 Author Share Posted May 22, 2007 but it's echoing everything twice. for example, if i echo $str_arr(2), it outputs "aa" instead of a. print_r($str_arr); also echoes 2 arrays Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258761 Share on other sites More sharing options...
jitesh Posted May 22, 2007 Share Posted May 22, 2007 Post your code. Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258762 Share on other sites More sharing options...
akitchin Posted May 22, 2007 Share Posted May 22, 2007 you can use the curly braces after a string variable to access only one character: <?php $string = 'hello'; echo $string{0}; $length = strlen($string) - 1; for($i = 0; $i <= $length; ++$i) { echo $string{$i}.'-'; } ?> Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258764 Share on other sites More sharing options...
play_ Posted May 22, 2007 Author Share Posted May 22, 2007 Post your code. I figured out. the code you have me is inside a loop. ill figure a way around it though. Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258767 Share on other sites More sharing options...
jitesh Posted May 22, 2007 Share Posted May 22, 2007 Check with print an array. <?php $str = "test"; $str_arr = array(); for($i=0;$i<strlen($str);$i++){ $str_arr[] = substr($str,$i,1); } echo "<pre>"; echo "This is the array with chars"; echo "<br>"; print_r($str_arr); ?> Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258769 Share on other sites More sharing options...
sasa Posted May 22, 2007 Share Posted May 22, 2007 tryfcode]<?php $a = 'word'; $b = str_split($a,1); print_r($b); ?> Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-258771 Share on other sites More sharing options...
chigley Posted May 22, 2007 Share Posted May 22, 2007 An easier way: <?php $string = "hello"; $chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); echo "<pre>" . print_r($chars, true) . "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/52438-explode/#findComment-259123 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.