Drezard Posted June 2, 2007 Share Posted June 2, 2007 Alright I'm writing a script where it splits a string into all of the characters. So here was part of my first script: $string = "Peter"; $explode = explode('', $string); I wanted to split the string into the characters, so i wanted the output of that code to be: $explode[0] = 'P'; $explode[1] = 'e'; $explode[2] = 't'; $explode[3] = 'e'; $explode[4] = 'r'; but it gives me this error: Warning: explode() [function.explode]: Empty delimiter. in /home/wintersw/public_html/encrypt.inc.php on line What other functions or code could I used to make this work? Thanks, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/53945-explode-problem/ Share on other sites More sharing options...
marcus Posted June 2, 2007 Share Posted June 2, 2007 <?php $string = "Peter"; $st = strlen($string); for($i=0;$i<$st;$i++){ echo $string{$i} . "<br>\n"; } ?> The for statement will emit: P E T E R But if you want each individual letter: P = $string{0} E = $string{1} T = $string{2} E = $string{3} R = $string{4} Quote Link to comment https://forums.phpfreaks.com/topic/53945-explode-problem/#findComment-266700 Share on other sites More sharing options...
registerpng Posted June 2, 2007 Share Posted June 2, 2007 Try the following code: $string = "Peter"; $explode = preg_split("//", $string); Quote Link to comment https://forums.phpfreaks.com/topic/53945-explode-problem/#findComment-266773 Share on other sites More sharing options...
wildteen88 Posted June 2, 2007 Share Posted June 2, 2007 You dont need to use any fancy code to get each letter seperatly. You can just do this: $str = 'Peter'; echo $str{0} . '<br />'; echo $str{1} . '<br />'; echo $str{2} . '<br />'; echo $str{3} . '<br />'; echo $str{4}; The result will be: P e t e r Quote Link to comment https://forums.phpfreaks.com/topic/53945-explode-problem/#findComment-266802 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.