How do you separate word into letters in php?

Asked

Viewed 460 times

0

I want to separate a string that has no separation in an array
example

$s = "123";
$array=["1","2","3"];

I tried to use it as follows valores = (array) explode("",$IdNota); but he gives Warning: explode(): Empty delimiter in /var/www/html/Model/model.php on line 572

Someone knows how I do?

  • 1

    And do you really need it? PHP already accepts string indexing, treating it as an array. Depending on the case, you can already use $s[0] ... etc without having to do any operation - See working: https://ideone.com/hkmwt8

4 answers

2


  • It is worth remembering that the function str_split is not secure against multi-byte characters

  • do not understand. can explain what would be multi-byte characters?

  • 1

    Unicode characters. Test for str_split("pão").

1

You can do it like this:

$string="123";
for( $i=0; $i < strlen($string) ; $i++ ){
   $array[$i]=$string[$i];
}

Go print something like this:

Array ( [0] => 1 [1] => 2 [2] => 3 ) 

1

You can use the function str_split to convert a string into an array.

Code Example:

<?php
$string = 123;
$split_length = 1;
$result = str_split ($string, $split_length);
?>

Upshot:

<?php
array (
 0 => '1',
 1 => '2',
 2 => '3',
);
?>

You can test at that link.

  • 1

    It is worth remembering that the function str_split is not secure against multi-byte characters

0

A simpler solution is using the preg_split, as below:

$valores = preg_split('//u', $IdNota,-1, PREG_SPLIT_NO_EMPTY);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.