Jump to content

explode()


play_

Recommended Posts

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
Share on other sites

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
Share on other sites

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