Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.