9
$str = "8560841836";
$mystr = array($str);
$string = strlen($str);
for($i=0; $i<=$string; $i++){   echo $string[$i]."\n";  }

This code print this string in one line but I want it to be print in one char in line and other so on...

Java Man
  • 1,854
  • 3
  • 21
  • 43
user2727842
  • 97
  • 1
  • 1
  • 2
  • 1
    possible duplicate of [PHP - iterate on string characters](http://stackoverflow.com/questions/4601032/php-iterate-on-string-characters) – joerick May 01 '14 at 10:11

6 Answers6

11

From the PHP documentation:

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. The functions substr() and substr_replace() can be used when you want to extract or replace more than 1 character.

Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose.

Warning Internally, PHP strings are byte arrays. As a result, accessing or modifying a string using array brackets is not multi-byte safe, and should only be done with strings that are in a single-byte encoding such as ISO-8859-1.

Examples:

// Get the first character of a string
$str = 'This is a test.';
$first = $str[0]; // 't'

// Get the third character of a string
$third = $str[2]; // 'i'

// Get the last character of a string.
$str = 'This is still a test.';
$last = $str[strlen($str)-1];  // '.'

// Modify the last character of a string
$str = 'Look at the sea';
$str[strlen($str)-1] = 'e'; // 'Look at the see'

So in your case, it is very easy:

$str = '8560841836';
$len = strlen($str);

for($i = 0; $i < $len; ++$i) // < and not <=, cause the index starts at 0!
    echo $str[$i]."\n";
Pang
  • 9,564
  • 146
  • 81
  • 122
Linblow
  • 449
  • 5
  • 7
6

There is function str_split('string'). It returns:

array(6) {
  [0]=>
  string(1) "s"
  [1]=>
  string(1) "t"
  [2]=>
  string(1) "r"
  [3]=>
  string(1) "i"
  [4]=>
  string(1) "n"
  [5]=>
  string(1) "g"
}

You can pass a second parameter for maximum length of chunk.

Example: str_split('string', 2) returns:

array(3) {
  [0]=>
  string(2) "st"
  [1]=>
  string(2) "ri"
  [2]=>
  string(2) "ng"
}

Documentation for str_split on php.net

David Wolf
  • 1,400
  • 1
  • 9
  • 18
MakoBuk
  • 622
  • 1
  • 10
  • 19
2

You confuse your string with your string length.

Moreover, you could use $string{$i} instead of $string[$i].

And, finally, be carrefull with the end of the loop ($i<$lenghtinstead of $i<=$lenght)

this works :

<?php
$str = "8560841836";
$lenght = strlen($str);
for($i=0; $i<$lenght; $i++){   
    echo $str[$i]."\n";
}
?>
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
  • 1
    @deceze `$str[$i]` works but if think it's recommended to use `$str{$i}`. I will look at it – Gwenc37 May 01 '14 at 10:36
  • No, it's neither recommended nor not recommended. http://www.php.net/manual/en/language.types.string.php#language.types.string.substr – deceze May 01 '14 at 10:38
0

Try using this code

$str = "8560841836";
$mystr = array($str);
$string = strlen($str);
for($i=0; $i<=$string; $i++){   echo $str[$i]."<br>";  }
Jatin Raikwar
  • 406
  • 5
  • 17
0
<?php
    $str = "Split Me !";
    $start = 0;
    for ($x = $start; $x < strlen($str); $x++) {
        echo $str[$x];
    }
?>
Simhadri
  • 61
  • 1
  • 5
-1

Use explode("", $data) (see this reference) to explode the string and iterate over the result.

$str = "8560841836";
$mystr = explode($str);
foreach( $mystr as $string){ 
    echo $string."\n"; 
}
Theolodis
  • 4,977
  • 3
  • 34
  • 53