$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...
$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...
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";
'T'
? –
Solenne 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"
}
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<$lenght
instead of $i<=$lenght
)
this works :
<?php
$str = "8560841836";
$lenght = strlen($str);
for($i=0; $i<$lenght; $i++){
echo $str[$i]."\n";
}
?>
$str[$i]
works but if think it's recommended to use $str{$i}
. I will look at it –
Dowitcher 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";
strlen()
should be the fastest method. –
Dasilva Try using this code
$str = "8560841836";
$mystr = array($str);
$string = strlen($str);
for($i=0; $i<=$string; $i++){ echo $str[$i]."<br>"; }
<?php
$str = "Split Me !";
$start = 0;
for ($x = $start; $x < strlen($str); $x++) {
echo $str[$x];
}
?>
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";
}
© 2022 - 2024 — McMap. All rights reserved.