add numbers to new lines separated by comma [duplicate] - php

This question already has answers here:
PHP counter increment in a while loop
(5 answers)
Closed 2 years ago.
i am having some issues with my php code. what i wish to add numbers like before every lines something like below
1. some data
2. some more data
what i have tried doing is like below but it would not multiply the numbers like i want it to, it only prints 1
$num = 1;
$tdetails = $num++. str_replace(',', '<br />', $row['travellers_details']);
Could someone please show how to achieve what i am looking for?
thanks

You need to iterate and increment num each time. You can store the elements in an array first using explode:
$num = 1;
$tdetails = explode(',', 'some data, some more data');
$str = "";
for($i = 0; $i < count($tdetails); $i++)
$str .= $num++ . ". ". $tdetails[$i] . "<br>";
echo $str;
Output:
1. some data
2. some more data

You can make use of HTML ol(ordered list) tag which will automatically do the numbering for you.
<?php
$tdetails = "<ol><li>" . implode("</li><li>", explode(",",$row['travellers_details'])) . "</li></ol>";
echo $tdetails;

Related

how to read for a certain symbol in a string [duplicate]

This question already has answers here:
What is the most efficient way to count all the occurrences of a specific character in a PHP string?
(4 answers)
Closed 5 years ago.
I am trying to separate 2 symbols from a string and then
count how many of these symbols there are.
So if i had : 1110001110000
Then it should find that there are 6= 1's and 7= 0's
So This is what I have tried:
Essentially what I need, is to read the string indexes in code would be string[$i] then IF there is a 1 or a 0 count it.
I tried using a for-loop
for ($i=0; $i < $getInput[$i] ; $i++) {
if ($getInput[$i] == 1) {
echo "ONE";
} elseif ($getInput[$i] == 0) {
echo "ZERO";
}
}
Here im trying to echo out ONE for everytime ther is a 1 and ZERO for everytime theres a zero.
$counter = 0;
foreach ($getInput as $key) {
echo $key;
}
here i tried to utilize a foreach, here I am not really declaring to see for One index, i tried putting a for each in a for but needless to say, it didn't work.
Using substr_count, you can do this in a fairly straightforward way:
echo substr_count("1110001110000", '1'); //Echos 6
echo substr_count("1110001110000", '0'); //Echos 7
Substr_count.
http://php.net/manual/en/function.substr-count.php
$str = "1110001110000";
Echo "there is " .substr_count($str, "0") ." zeros \n";
Echo "there is " .substr_count($str, "1") ." ones \n";
https://3v4l.org/BQEiR
If you want to output it as your code implies (one one one zero) you can use numberformatter.
Here I split the string to an array and loop through it and output the spellout of each number.
$str = "1110001110000";
$arr = str_split($str);
$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
Foreach($arr as $numb){
echo $nf->format($numb) . " ";
}
Output:
one one one zero zero zero one one one zero zero zero zero
https://3v4l.org/nHX59

Php count list of digits [duplicate]

This question already has answers here:
Get the sum of all digits in a numeric string
(13 answers)
Closed 5 years ago.
I am getting o/p like "11111" and I want to sum all these digits that should become 5. But if I use count count it is showing one only i.e, 1.Rather it should show 5.
Below is my code,
$count = count($inventory['product_id']);
$product_total = $count;
echo $product_total;//o/p => 1.
I need echo $product_total;//o/p => 5.
You can use the following using str_split to get an array with all characters (in your case digits) and using array_sum to get the sum of all the digits:
$digits = "11112";
$arrDigits = str_split($digits);
echo array_sum($arrDigits); //6 (1 + 1 + 1 + 1 + 2)
Demo: https://ideone.com/tZwi9J
Count is used for counting array elements.
What you can do in PHP, is to iterate over a string using either a foreach (not 100% sure) or for loop for this and accessing the elements like array elements by their index:
$str = '111111123545';
$sum = 0;
for ($i = 0; $i < strlen($str); $i++) {
$sum += intval($str[$i]);
}
print $sum; // prints 26
Alternativly, you can split the string using no delimiter and using the array_sum() function on it:
$str = '111111123545';
$sum = array_sum(str_split($str));
print $sum; // prints 26
array_sum(str_split($number));
Another possible way to count the list of digits in PHP is:
// match only digits, returns counts
echo preg_match_all( "/[0-9]/", $str, $match );
// sum of digits
echo array_sum($match[0]);
Example:
$ php -r '$str="s12345abas"; echo "Count :".preg_match_all( "/[0-9]/", $str, $match ).PHP_EOL; echo "Sum :".array_sum($match[0]).PHP_EOL;'
Count :5
Sum :15

Incrementing numbers with double digits in php [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 7 years ago.
I would like to Increment numbers with double digits if the number is less then 10
This is what i tried so far
$i = 1;
echo $i++;
results is 1,2,3,4,5,6 so on
Then i try adding a condition
$i = 1;
if ($i++<10){
echo "0".$i++;
}else{
echo $i++;
}
Work but skipping the numbers 2,4,6,8 so on.
Can anyone tell me the proper way to do this?
If the condition is only there for the leading zero you can do this much easier with this:
<?php
$i = 10;
printf("%02d", $i++);
?>
if you want prepend something to a string use:
echo str_pad($input, 2, "0", STR_PAD_LEFT); //see detailed information http://php.net/manual/en/function.str-pad.php
On the second fragment of code you are incrementing $i twice, that's why you get only even numbers.
Incrementing a number is one thing, rendering it using a specific format is another thing. Don't mix them.
Keep it simple:
// Increment $i
$i ++;
// Format it for display
if ($i < 10) {
$text = '0'.$i; // Prepend values smaller than 10 with a zero
} else {
$text = $i;
}
// Display it
echo($text);
<?php
$i = 1;
for($i=1;$i<15;){
if($i<10){
echo '0'.$i++."<br>";
}else{
echo $i++."<br>";
}
}
?>

php code to display specific amount of words in rows [duplicate]

This question already has answers here:
MySQL/SQL retrieve first 40 characters of a text field?
(5 answers)
Closed 8 years ago.
table name - animals
column name id, story
I am inserting certain amount of data in story column.
But I need to display only first 20 characters in row. plz suggest me how to display only specific amount of characters.
below is my code... to echo rows.
<?php foreach($rows as $row){ ?>
<?php echo htmlspecialchars_decode($row['story']); ?>
<?php } ?>
You can use the explode function, and itarate thorugh words:
foreach ($rows as $row) {
$line = htmlspecialchars_decode($row['story']);
$words = explode(" ", $line);
$newWords = array();
for ($i = 0; $i < 20; $i++) {
if (isset($words[$i])) {
$newWords[] = $words[$i];
}
}
echo join(" ", $newWords)."<br>";
}

PHP split a very big number [duplicate]

This question already has answers here:
What's HTML character code 8203?
(8 answers)
Closed 8 years ago.
I used a number below in my code:
$MyString = '06887558108616348​33464996​60139294';
When i'm trying to split MyString into a same pieces for example: 06887558, 10861634 so on... using substr or str_split that gives me:
06887558, 10861634, 8​3346, 4996​6, 0139294
Someone explain why this happend?!?
The Code what I have tried
$MyNewString; $n = 8; // How many you want before seperation
$MyNewString = substr($MyString,0,$n);
$i = $n;
while ($i < strlen($MyString)) {
$MyNewString .= '-'; // Seperator Character
$MyNewString .= substr($MyString,$i,$n);
$i = $i + $n;
}
echo $MyNewString;
add charset utf-8
add this code
echo '<meta charset="UTF-8">';
see the result I have tried
$new_string = implode(', ', str_split($n, 8));
echo $new_string;
If you're getting funny characters in the result, then they must have been in the original string. substr and str_split don't add anything.

Categories