Jump to content

Explode Problem


Drezard

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/53945-explode-problem/
Share on other sites

<?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}

Link to comment
https://forums.phpfreaks.com/topic/53945-explode-problem/#findComment-266700
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/53945-explode-problem/#findComment-266802
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.