I have textfile "animals.txt" which looks like:
mammals cat, fox, horse
birds parrot, eagle
reptiles snake, crocodile
Columns are separated with \t and ,.
How can I display it as nested list looking like this:
<ul>
<li>mammals
<ul>
<li>cat</li>
<li>fox</li>
<li>horse</li>
</ul>
</li>
<li>birds
<ul>
<li>parrot</li>
<li>eagle</li>
</ul>
</li>
<li>reptiles
<ul>
<li>snake</li>
<li>crocodile</li>
</ul>
</li>
</ul>
What I have now:
$data = file_get_contents('animals.txt');
$array = explode("\t", $data);
foreach ($array as $line) {
echo "<li>$line</li>";
}
This will work. Exploding each line and then printing the corresponding ul's and li's:
$file = file('animals.txt');
print '<ul>';
foreach ($file as $line) {
// 1) exploding by "\t" and then again by ","
$values = explode("\t", $line);
$list = explode(',', $values[1]);
// 2) show the first name in the line
print '<li>';
print $values[0];
print '<ul>';
// 3) followed by a nested ul containing the comma-separated values
foreach ($list as $sub_item) {
print '<li>' . $sub_item . '</li>';
}
print '</ul>';
print '</li>';
}
print '</ul>';
This will work. The first step is to separate values that are tab delimited(mammals cat, fox, horse) and second step(foreach loop) is to get comma delimited values(cat, fox, horse). Finally we are printing individual values in a list(cat fox etc).
echo "<ul>";
$array = explode("\t", $data);
echo "<li>".$array[0];
foreach ($array[1] as $line) {
$values = explode(",",$line)
echo "<ul>"
foreach ($values as $val) {
echo "<li>$val</li>";
}
echo "<.ul>"
}
echo "</li>";
echo "</ul>";
Related
Not a php guru here.
I have a string:
works/but/needs/splitting
I'd need the output in a ul list
<ul>
<li>works</ul>
<li>but</li>
<li>needs</li>
<li>splitting</li>
<ul>
I have been looking into explode("/", $text); and tried
$originalstring = "works/but/needs/splitting";
$delimiter = "/";
if(strpos($originalstring,$delimiter) > 0){
But I just don't know much of php and I can't work it out
Sorry I might not understand your questions, But split by / is this
$originalstring = "works/but/needs/splitting";
$pieces = explode("/", $originalstring);
echo '<ul>';
foreach ($pieces as $pi){
echo '<li>'.$pi.'</li>';
}
echo '</ul>';
$originalstring = "works/but/needs/splitting";
$e=explode('/',$originalstring); //creates the array ($e) of each element
echo '<ul>';
foreach ($e as $each){ //loop the array ($e)
echo '<li>'.$each.'</li>';
}
echo '</ul>';
I have file
animals.txt
fishes shark,tuna,salmon
birds parrot,pigeon
mammals cow,dog,cat
In every line, for example between fishes and shark is tabulation.
I wanna get this output:
<ul type="disc">
<li>fishes<br>
<ul type="disc">
<li>shark</li>
<li>tuna</li>
<li>salmon</li>
</ul>
</li>
<li>birds<br>
<ul type="disc">
<li>parrot</li>
<li>pigeon</li>
</ul>
</li>
<li>mammals<br>
<ul type="disc">
<li>cow</li>
<li>dog</li>
<li>cat</li>
</ul>
</li>
</ul>
I wrote simple code, but I don't know what can I do next, can someone help me solve it?
index.php
$file = fopen('animals.txt', 'r');
while (!feof($file)) {
$line = fgets($file);
$a = explode(" ", $line);
$b = str_replace(",", " ", $a);
$c = explode(" ", $b);
print_r($b);
}
fclose($file);
?>
If you're simply going to read that file and not write in it, maybe using file will be simpler than fopen as it creates an array of lines.
You'll want to double explode every line, once on the tabulation to separate family and animals and a second time to split animals individually.
To create a nicely structure array of families and animals, try the following and add some var_dump at different steps to see the logic:
$file = file('animals.txt');
$groupedAnimals = array();
foreach ($file as $line) {
// First explode on tabulations
$line = explode("\t", $line);
$family = $line[0];
// Then explode on commas
$animals = explode(',', $line[1]);
foreach ($animals as $animal) {
$groupedAnimals[$family][] = $animal;
}
}
The groupedAnimals array should be populated like that:
Array
(
[fishes] => Array
(
[0] => shark
[1] => tuna
[2] => salmon
)
[birds] => Array
(
[0] => parrot
[1] => pigeon
)
[mammals] => Array
(
[0] => cow
[1] => dog
[2] => cat
)
)
Once the groupedAnimals array is populated, you can simply print them the way you want, following your template:
<ul type="disc">
<?php foreach ($groupedAnimals as $family => $animals): ?>
<li>
<?php echo $family ?>
<ul>
<?php foreach ($animals as $animal): ?>
<li><?php echo $animal ?></li>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>
function xxAxx(){
$output='';
$file = fopen('animals.txt', 'r');
while (!feof($file))
{
$line = fgets($file);
//replace white spaces with a different delimiter
$line = preg_replace('/\s+/', '-', $line);
$line = trim($line);
// explode with your delemeter
$a = explode("-", $line);
$category = $a[0];
$categoryItems = explode(",", $a[1]);
$output .= "<li>".$category ."<br>";
$output .= "<ul type='disc'>";
foreach ($categoryItems as $item) {
$output .= '<li>'.$item.'</li>';
}
$output .= "</ul>";
$output .= "</li>";
}
fclose($file);
return $output;
}
?>
<ul type="disc">
<?php echo xxAxx(); ?>
</ul>
I have a set of numbers in a table field in database, the numbers are separated by comma ','.
I am trying to do the following:
Step 1. : SELECT set of numbers from database and explode it to array :
$array = explode(',', $set_of_numbers);
Step 2. : Print each element of the array as list item by using foreach loop :
foreach ($array as $list_item => $set_of_numbers){
echo "<li>";
print_r(array_list_items($set_of_numbers));
echo "</li>";}
Please anybody tell me what is wrong. Thank you.
$numbers = '1,2,3';
$array = explode(',', $numbers);
foreach ($array as $item) {
echo "<li>$item</li>";
}
Assuming your original $set_of_numbers is simply a CSV string, something like 1,2,3,4,..., then your foreach is "mostly" ok. But your variable naming is quite bonkers, and your print-r() call uncesary:
$array = explode(',', $set_of_numbers);
foreach($array as $key => $value) {
echo "<li>$key: $value</li>";
}
Assuming that 1,2,3,4... string, you'd get
<li>0: 1</li>
<li>1: 2</li>
<li>2: 3</li>
etc...
$numbers = "1,2,3";
$array = explode(",", $numbers);
/* count length of array */
$arrlength = count($array);
/* using for while */
$x = 0;
while ($x < $arrlength) {
echo "<li>$array[$x]</li>" . PHP_EOL;
$x++;
}
echo PHP_EOL;
/* using for classic */
for ($x = 0; $x < $arrlength; $x++) {
echo "<li>$array[$x]</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc */
foreach ($array as $value) {
echo "<li>$value</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc key */
foreach ($array as $key => $value) {
echo "<li>$key => $value</li>" . PHP_EOL;
}
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/ZqT4Yi" ></iframe>
You actually don't need to explode on the commas. You can just replace each comma with an ending tag followed by an opening tag and then wrap your whole string in an opening and closing tag.
$set_of_numbers = '1,2,3';
echo '<li>' . str_replace(',', '</li><li>', $set_of_numbers) . '</li>';
// outputs: <li>1</li><li>2</li><li>3</li>
No looping is necessary.
Here is answer for your question to get ride of your problem
$Num = '1,2,3,4,5,';
$Array = explode(',',$Num);
foreach ($Array as $Items)
{
echo "<li>&Items</li>"; // This line put put put in the list.
}
This can easily be achieved by the following code snippet:
<?php
$my_numbers = '1,12,3.2,853.3,4545,221';
echo '<ul>';
foreach(explode(',', $my_numbers) AS $my_number){
echo '<li>'.$my_number.'</li>';
}
echo '</ul>';
The above code will output the following HTML:
<ul><li>1</li><li>12</li><li>3.2</li><li>853.3</li><li>4545</li><li>221</li></ul>
Credits: http://dwellupper.io/post/49/understanding-php-explode-function-with-examples
I have a field in my database with the text value:
"these, are, some, keywords" (minus the inverted commas)
Now, I wonder if I can generate an unordered list from this so ultimately my HTML reads:
<ul>
<li>these</li>
<li>are</li>
<li>some</li>
<li>keywords</li>
</ul>
Is this possible with PHP and if so is anyone able to help me out with this?
Many thanks for any pointers.
You can accomplish this with something like the following:
<?php
$yourList = "these, are, some, keywords";
$words = explode(',', $yourList);
if(!empty($words)){
echo '<ul>';
foreach($words as $word){
echo '<li>'.htmlspecialchars($word).'</li>';
}
echo '</ul>';
}
?>
As mentioned by elcodedocle, you may want to use str_getcsv() instead of explode if more appropriate.
Have a look at str_getcsv() and explode()
Example:
<?php
$mystring = "these, are,some , keywords";
$myvalues = str_getcsv($mystring);
$myoutput = "<ul>";
foreach ($myvalues as $value){
$myoutput .= "<li>".trim($value)."</li>\n";
}
$myoutput .= "</ul>";
echo $myoutput;
?>
You need to explode you string for ', '
print <ul>
for each element in the array you received you print '<li>' . $value . '</li>'
print </ul>
You can try:
$arr = explode(",","these, are, some, keywords");
$res = "<ul>";
foreach ($arr as $val){
$res .= "<li>" . $val . "</li>";
}
$res .= "</ul>";
echo $res;
I have a variable on my page called, $recipe['ingredients'];
inside the var you have as follows,
100ml milk, 350ml double cream, 150ml water
and so on. Now I'm trying to split it up so it looks as follows
<ul>
<li>100ml milk</li>
<li>350ml double cream</li>
<li>150ml water</li>
</ul>
So far I have the following code,
$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
$ingredients = array($ingredientsParts);
while (! $ingredients) {
echo" <li>$ingredients</li>";
}
But for some reason it doesn't work and I do not have the experience with explode to fix it.
$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
$li = '<ul>';
foreach($ingredientsParts as $key=>$value){
$li.=" <li>$value</li>";
}
$li.= '</ul>';
echo $li;
this should be enough:
$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);
foreach ($ingredientsParts as $ingredient)
{
echo "<li>$ingredient</li>";
}
or you can explode it by ',' and use echo '<li>' . trim($ingredient) . '</li>'; to remove whitespace from beginning/end of that string
When you explode() a string it is automatically converted into an array. You do not need to convert it to an array type as you did on the second line.
You want to use a foreach() loop to iterate through an array, not a while loop.
$ingredientsAry = explode(',', $row_rs_recipes['ingredients']);
foreach($ingredientsAry as $ingredient){
echo "<li>$ingredient</li>";
}
In fact you can just do a foreach() loop on the explode() value
foreach(explode(',', $row_rs_recipes['ingredients']) as $ingredient){
echo "<li>$ingredient</li>";
}
The explode method already return an array, so you don't have to transform your variable $ingredientsParts into an array.
Just do:
$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);
foreach ($ingredientsParts as $ingredient)
{
echo "<li>$ingredient</li>";
}
if (!empty($recipe['ingredients'])) {
echo '<ul><li>' . implode('</li><li>', explode(', ', $row_rs_recipes['ingredients'])) . '</li></ul>';
}
You can do this:
$ingredients = explode(',', $row_rs_recipes['ingredients']);
$list = '<ul>';
foreach ($ingredients as $ingredient)
{
$list .= '<li>' . $ingredient . '</li>';
}
$list .= '</ul>';
echo $list;