$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...
The substr() function used to cut a part of a string from a string, starting at a specified position. The input string. Refers to the position of the string to start cutting. A positive number : Start at the specified position in the string.
The substr() is a built-in function of PHP, which is used to extract a part of a string. The substr() function returns a part of a string specified by the start and length parameter. PHP 4 and above versions support this function.
The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With