PHP: How to get characters of string one by one
Asked Answered
S

7

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

Schinica answered 1/5, 2014 at 10:4 Comment(1)
possible duplicate of PHP - iterate on string charactersBismuthous
M
13

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";
Meteoritics answered 1/5, 2014 at 10:35 Comment(4)
PHP has str_split () function, which is used to print every character of a string. No need to use any loop. @user3414654 answer is right.Veron
Nice pre-php5 script though.Rebozo
C habits tend to live forever!Meteoritics
Is there a reason $first isn't 'T'?Solenne
A
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

Appendant answered 18/5, 2018 at 7:30 Comment(0)
D
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";
}
?>
Dowitcher answered 1/5, 2014 at 10:24 Comment(2)
@deceze $str[$i] works but if think it's recommended to use $str{$i}. I will look at itDowitcher
No, it's neither recommended nor not recommended. php.net/manual/en/…Outgeneral
D
1

I know it's a "bit" of late, but I was wondering why nobody shared the following method (maybe it's too recent or bad, idk):

$str = "8560841836";
for ($i = 0; isset($str[$i]); $i++)
    echo "$str[$i]\n";
Dasilva answered 16/8 at 10:13 Comment(1)
Anyway, I did some (testing)[gist.github.com/Aldisti/7a8b12ddd9af544a86a4d195be3c50dc] just to know which method was better. Using strlen() should be the fastest method.Dasilva
G
0

Try using this code

$str = "8560841836";
$mystr = array($str);
$string = strlen($str);
for($i=0; $i<=$string; $i++){   echo $str[$i]."<br>";  }
Glade answered 14/4, 2016 at 5:29 Comment(0)
M
0
<?php
    $str = "Split Me !";
    $start = 0;
    for ($x = $start; $x < strlen($str); $x++) {
        echo $str[$x];
    }
?>
Mate answered 13/11, 2020 at 16:26 Comment(0)
P
-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"; 
}
Persecute answered 1/5, 2014 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.