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

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>";
}

Related

add numbers to new lines separated by comma [duplicate]

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;

Split a number by 13 digits using php [duplicate]

This question already has answers here:
Break long string into pieces php
(5 answers)
Closed 3 years ago.
I want to make a program which splits a long number into pieces of 13 digited number such that I can loop for every 13 digits just using php.
$number = 012345678901230123456789123
Should output
0123456789123
0123456789123
And it should be for any large number having the number of digit multiple of 13.It looks about looping and algorithm but I want to make it as short as possible and I have doubts on how to do it. So I am just asking about the main concept.
The most dynamic solution is probably to use array_functions on the string.
So str_split to make it array then chunk it in size 13 and implode the arrays.
$number = "012345678901230123456789123";
$arr = array_chunk(str_split($number), 13);
foreach($arr as &$val){
$val = implode($val);
}
https://3v4l.org/LsNFt
You can create a function where you can use your string and size as parameter and return an array of strings of the desired length:
function splitString($str, $packetSize) {
$output = [];
$size = strlen($str);
for ($i = 0; $i < $size; $i += $packetSize) {
if ($i + $packetSize < $size) {
$output[]= substr($str, $i, $packetSize);
} else {
$output[]=substr($str, $i);
}
}
return $output;
}

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>";
}
}
?>

Random Numbers, Column and Row. PHP

Please excuse me as this is my first post and I am fairly new to any type of programming. I hope my question is clear, I am using Excel references as I think this explains what I am trying to do best.
I am trying to generate random numbers for a pool. I have 8 rows of numbers and each row contains 10 spots, 0 to 9. I want to have a random number in each row and make sure the the number does not repeat in each row.
Example Grid - 8 columns wide x 10 rows long.
I am repeating this scrip for each column, but I am getting the same number is rows and I want make sure that does not happen.
for ($i=1; $i<=10; $i++) {
while (1) {
$duplicate = 0;
$num=rand(0,9);
for ($x=1; $x<$i; $x++) {
if ($NFC1[$x]==$num) { $duplicate = 1; }
}
if ($duplicate==0) {
$NFC1[$i]=$num;
break;
}
}
}
This is the results, as you can see I have random numbers is each column but not in each row.
"4";"8";"5";"5";"0";"4";"2";"7"
"5";"9";"4";"3";"9";"9";"9";"0"
"9";"5";"1";"1";"5";"8";"6";"1"
"7";"4";"6";"2";"6";"7";"3";"3"
"2";"6";"8";"4";"7";"2";"7";"5"
"0";"1";"0";"7";"2";"1";"4";"6"
"1";"7";"9";"9";"4";"3";"0";"4"
"3";"0";"3";"0";"3";"5";"5";"9"
"8";"2";"7";"8";"1";"6";"8";"2"
"6";"3";"2";"6";"8";"0";"1";"8"
The answer from here addapted to non-square array may looks like below:
$rows = 10; // Number of rows
$columns = 8; // Number of columns
$row = range(0, $columns-1);
$column = range(0, $rows-1);
shuffle($row);
shuffle($column);
// Create an array
foreach ($row as $x => $value)
foreach ($column as $y)
$array[$y][$x] = $value++ % max($rows, $columns);
And if you want to see the result:
foreach($array as $r) {
foreach($r as $number) {
echo $number.' ';
}
echo "<br/>";
}

Split while loop into multiple parts [duplicate]

This question already has answers here:
PHP loop: Add a div around every three items syntax [duplicate]
(4 answers)
Closed 9 years ago.
I want to wrap every 10-groups of items in a while-loop inside a wrapper.
Visualized:
echo "<ul class='wrapper'>";
while(get_field('items'))
{
echo "<li>item</li>";
}
echo "</ul>";
In this case every element would be inside this one wrapper, but I have to wrap at max ten elements and then start a new wrapper.
What would be the best way to accomplish this?
You can try this
$count=1;
echo "<ul class='wrapper'>";
while(get_field('items'))
{
if($count % 10 == 0) {echo '</ul><ul class='wrapper'>';}
echo "<li>item</li>";
$count++;
}
echo "</ul>";
A different way to do this is:
<?php
$count = 0;
$group = array();
while(get_field('items'))
{
array_push($group, "<li>$val</li>");
if(++$count % 10 == 0)
{
echo "<ul class='wrapper'>".implode("", $group)."</ul>";
$group = array();
}
}
?>

Categories