Re: Character Array
| From: | David Otton | Date: | Thu, 11 Jul 2002 12:06:56 +0000 |
| Subject: | Re: Character Array | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-107105@lists.php.net to get a copy of this message | ||
On Thu, 11 Jul 2002 13:50:59 +0200, you wrote:
>> >Is there an easy way to get an array of characters from a string?
>> $str = 'string';
>> $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
>> print_r($chars);
>
>A string is already an array of chars :
>$toto="test";
>echo $toto[0]; // prints t
>echo $toto[1]; // prints e
>echo $toto[2]; // prints s
>echo $toto[3]; // prints t
Not really; a string is better described as a type that has some
properties in common with the array type. Consider these two examples:
<?
$str = "Hello World!";
$str = array_reverse($str);
echo ($str);
?>
<?
$str = "Hello World!";
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
$str = implode('', array_reverse($chars));
echo ($str);
?>
djo