How could I split a number from end to start?
Considering that the number can have any length.
The closest thing I tried:
$str = "1234"; #or any other number/string ranging from 0 - 99999
#then somehow inverse the number
$arr1 = str_split($str);
if(!empty($arr1[0])){echo "<div ...>".$arr1[0]."</div>";}
if(!empty($arr1[1])){echo "<div ...>".$arr1[1]."</div>";}
if(!empty($arr1[2])){echo "<div ...>".$arr1[2]."</div>";}
if(!empty($arr1[3])){echo "<div ...>".$arr1[3]."</div>";}
if(!empty($arr1[4])){echo "<div ...>".$arr1[4]."</div>";}
Thank you.
Just use strrev function like this:
<?php
$rev = strrev($str);
?>
Update: One liner to do it all:
$reverse =
array_map(create_function('$n','return "<div...>$n</div>";'), str_split(strrev($str)));
$number = 1234567;
$number = str_split($n);
$numbers = array_reverse($number);
The $number variable is converted to a string if it is not already before being split. Then the array is reversed.
To loop through it:
foreach ($numbers as $value) {
echo "<div ...>".$value."</div>";
}
You can find the length and using the for loop you can extract the numbers one by one.
You can find the length using strlen() and for loop upto strlen()
$str = "1234";
$str = strrev($str);
for($i=0; $i<= strlen($str); $i++){
echo substr($str,$i,1)."<br>";
}
Related
I want to iterate a string in the manner that after each character there should be a space and there will be new string(word) as per the main string character count.
For example
If I put the string "v40eb" as an input. Then Output be something like below.
v 40eb
v4 0eb
v40 eb
v40e b
OR
In Array form like below.
[0]=>v 40eb[1]=>v4 0eb[2]=>v40 eb[3]=>v40e b
I am using PHP.
Thanks
Well, you can divide the process of putting a space into 2 parts.
Get first part of the substring, append a space.
Get second part of the substring and join them together.
Use substr() to get a substring of a string.
Snippet:
<?php
$str = "v40eb";
$result = [];
$len = strlen($str);
for($i=0;$i<$len;++$i){
$part1 = substr($str,0,$i+1);
if($i < $len-1) $part1 .= " ";
$part2 = substr($str,$i+1);
$result[] = $part1 . $part2;
}
print_r($result);
Demo: https://3v4l.org/XGN0a
You could simply loop over the char positions and use substr to get the two parts for each:
$input = 'v40eb';
$combinations = [];
for ($charPos = 1, $charPosMax = strlen($input); $charPos < $charPosMax; $charPos++) {
$combinations[] = substr($input, 0, $charPos) . ' ' . substr($input, $charPos);
}
print_r($combinations);
Demo: https://3v4l.org/EeT1V
$input = 'v40eb';
for($i = 1; $i< strlen($input); $i++) {
$array = str_split($input);
array_splice($array, $i, 0, ' ');
$output[] = implode($array);
}
print_r($output);
Dont forget to check the codec. You might use mb_-prefix to use the multibyte-functions.
Is it possible to split a string using foreach?
For example my string value is 10
using foreach echos 1,
then echo 2,
then echo 3,
and so on to till it reach the string value.
Yes you can but looking at what you try to achieve, you really don't need to split instead you are trying to increment using loop till the string value reached. In PHP you can cast string to integer and by default PHP will do that for you if you are trying to perform + - * % / operations.
Using Foreach
$string = '10';
$numbers = range(1, $string);
foreach($numbers as $number){
echo $number;
}
Using For
$string = '10';
for($i = 1; $i <= $string; $i++){
echo $i;
}
If splitting string is what your objective, you can use str_split() function and loop like the above foreach()
I'm trying to write a function that would take the $s parameter and split it into letters thus creating an array of the letters of $s.
$n is the letter counter, so it would loop up to 10 letters and return EricEricEr.
The first issue: I understand I'm creating an array inside an array, and since I would like to receive the string of the inner array, I would need to implode it twice.
The Second issue: I'm unable to print the 10 first letters, only the word 10 times (i.e. EricEricEricEric...10 times).
Expected Result: I would like the for loop i to go over the letters, and when reaching $n and print all the letters with no glue, just consecutively, receiving the result of this specific example function params as: EricEricEr.
function letterCounter($s, $n){
for ($i = 0; $i<=$n; $i++){
$split [] = str_split($s);
}
$imploded = implode($split);
return $imploded;
}
$result = letterCounter('Eric', 10);
echo '<pre>';
print_r($result);
echo '</pre>';
This will do but you can improve the code
function letterCounter($s, $n)
{
$split = str_split($s); //array of splitted $s into letters
$count=0;
$str = "";
while($count<$n){
foreach($split as $char){
if($count>=$n)
break;
$str.= $char;
$count++;
}
}
return $str;
}
$result = letterCounter('Eric', 10);
echo '<pre>';
print_r($result);
echo '</pre>';
Not sure if I got you right here, but PHP got everything you need:
$string = "Eric";
str_pad($string, 10, $string);
"EricEricEr"
I have a string of delimited numerical values just like this:
5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004| ...etc.
Depending on the circumstance, the string may have only 1 value, 15 values, all the way up to 100s of values, all pipe delimited.
I need to count off (and keep/echo) the first 10 values and truncate everything else after that.
I've been looking at all the PHP string functions, but have been unsuccessful in finding a method to handle this directly.
Use explode() to separate the elements into an array, then you can slice off the first 10, and implode() them to create the new string.
$arr = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
$a = explode ('|',$arr);
$b = array_slice($a,0,10);
$c = implode('|', $b);
Use PHP Explode function
$arr = explode("|",$str);
It will break complete string into an array.
EG: arr[0] = 5, arr[1] = 2288 .....
I would use explode to separate the string into an array then echo the first ten results like this
$string = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
$arr = explode("|", $string);
for($i = 0; $i < 10; $i++){
echo $arr[$i];
}
Please try below code
$str = '5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324';
$arrayString = explode('|', $str);
$cnt = 0;
$finalVar = '';
foreach ($arrayString as $data) {
if ($cnt > 10) {
break;
}
$finalVar .= $data . '|';
$cnt++;
}
$finalVar = rtrim($finalVar, '|');
echo $finalVar;
I have a range of number, say that it from 1 to 10.
//$front_id = 1;
//$until_id = 10;
foreach (range($front_id, $until_id) as $number)
{
print_r($number)
//12345678910
}
It print a range of number between two variables as expected.
However, in the print result is it combine all number without any delimiter.
So, I tried to add a delimiter of comma here:
$numbers = implode(',', $number);
But it doesn't work.
Message: implode(): Invalid arguments passed
You can store $number in array. Right now $number is not an array. That why you cannot implode. Define array first. Then inside foreach() you can store every $number in $numbers array.
$front_id = 1;
$until_id = 10;
$numbers = array();
foreach (range($front_id, $until_id) as $number)
{
$numbers[] = $number;
}
echo implode(',',$numbers);