PHP - List pulled from one field exploding commas - php

The title may be a bit misleading, to be honest I don't know what this feature is called.
I have a field which will contain a list of a users skills in the database, the skills will be separated by commas.
When I then echo the field, I want each skill after the commas to be in a new list element.
So it'll echo like this in the HTML:
<ul>
<li>Cycling</li>
<li>Driving</li>
<li>Running</li>
</ul>
However in the field 'skills' it'll look like:
Cycling, Driving, Running
I read that you need to explode the comma however I have no idea how to accomplish it.

Here's explode:
$activities = explode(',', $activities);
Filter to trim the values of the array (so there's no whitespace on them) using array_map:
$activities = array_map('trim', $activities);
Then glue it back up with implode:
$activities = '<li>' . implode('</li><li>' . $activities) . '</li>';
Put it all together, with a bit of defensive programming:
$activities = explode(',', $activities);
if ( $activities ) {
$activities = array_map('trim', $activities);
$activities = '<li>' . implode('</li><li>' . $activities) . '</li>';
}

You could use str_replace:
$skills = "Cycling, Driving, Running";
echo '<ul><li>' . str_replace(", ", "</li><li>", $skills) . '</li></ul>';

This can be achieved with explode (php documentation).
$activities = 'Cycling, Driving, Running';
$exploded = explode(', ', $activities);
foreach ($exploded as $activity) {
echo '<li>' . $activity . '</li>';
}

$list = "Cycling, Driving, Running";
$elements = explode(", ",$list);
echo "<ul>";
foreach($elements as $element){
echo "<li>$element</li>";
}
echo "</ul>";

I've got it working now! :)
Thanks to who contributed.
Code I used:
<ul class="list-unstyled">
<?php
$activities_q = "SELECT skills FROM users WHERE uid='".$row['uid']."'";
$activities = $row['skills'];
$exploded = explode(',', $activities);
foreach ($exploded as $activity) {
echo '<li class="list-group-item">' . $activity . '</li>';
}
?>
</ul>

Related

How to split a string every certain character?

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>';

Generate a set of HTML list items from a comma separated list? PHP

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;

PHP - Split an array into chunks.

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;

Need to create li with list of different links using php explode method

here is my working php explode code for NON links:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li>' . $item . '</li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
..and the code which defines "my_custom_output", which I input into my textarea field:
text1,text2,text3,etc
..and the finished product:
text1
text2
text3
etc
So that works.
Now what I want to do is make text1 be a link to mywebsite.com/text1-page-url/
I was able to get this far:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li class="link-class"><a title="' . $item . '" href="http://mywebsite.com/' . $item_links . '">' . $item . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
Now, I would like $item_links to define just the rest of the url. For example:
I want to put input this into my textarea:
text1:text1-page-url,text2:new-text2-page,text3:different-page-text3
and have the output be this:
text1
text2
..etc (stackoverflow forced me to only have two links in this post because I only have 0 reputation)
another thing I want to do is change commas to new lines. I know the code for a new line is \n but I do not know how to swap it out. That way I can put this:
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
I hope I made this easy for you to understand. I am almost there, I am just stuck. Please help. Thank you!
Just split the $item inside your loop with explode():
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And choose your string separators so $item_text, $item_links wouldn't contain them.
After you explode the string you could loop through again and separate the text and link into key/values. Something like...
$array_final = new array();
$array_temp = explode(',', $textarea);
foreach($array_temp as $item) {
list($text, $link) = explode(':', $item);
$array_final[$text] = $link;
}
foreach($array_final as $key => $value) {
echo '<li>'.$key.'</li>';
}
to change comma into new line you can use str_replace like
str_replace(",", "\r\n", $output)

Take input from text box and parse each line into bullet list

$method1 = '<ul><li>' . implode('</li><li>', explode("\n", $method)) . '</li></ul>';
I have this code here and i am trying to parse the input so that each new line that is entered into a text box is parsed to a bullet list, but for some reason this code is only doing the first line
Example:
do this
do this
turns into this inside a single variable:
<ul>
<li>do this</li>
<li>do this</li>
</ul>
Do you mean this (I've used your variable names):
// explode
$list = explode("\n", $method);
// iterate
$method1 = "<ul>";
foreach ($list as $item) {
$method1 .= "<li>" . $item . "</li>";
}
$method1 .= "</ul>";
// output
echo $method1;
Somethimes \n doesnt really recognize as new line in php.
try this:
$method1 = '<ul><li>' . implode('</li><li>', explode("
", $method)) . '</li></ul>';

Categories