Home > Net >  How can I generate a set of HTML list items from \n newline separated string using php?
How can I generate a set of HTML list items from \n newline separated string using php?

Time:12-13

How can I generate a set of HTML list items from \n newline separated string using php?

I'm using the Google Sheet PHP API with a foreach loop

// authentication not shown
$range = 'Sheet3!AD2:Z';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();

foreach($values as $val) {
$valuespan = "<span class=\"record\">".implode($val)."</span>";
echo $valuespan;
}

to get the HTML output of all rows in the Google Sheet wrapped in span markup like this: (the \n newlines are shown for illustration)

<span >Customer 1 \n
Exercise Physiologist \n
PT, DPT, COMT \n
Class Name \n
South Physical Therapy \n
City 1 \n
AR \n
</span>


<span >Customer 2 \n
Exercise Physiologist \n
PT \n
Class Name \n
Central Therapy \n
City 2 \n
AL \n
</span>

...on and on to the last row of the Sheet.

I need to be able to use the \n delimiter to add markup, as I can't use a comma, due to commas being in the data; and I can't add any other delimiters to the Sheet.

So what I need is this specific markup; I need each Customer in <h1> tags and the other items in <li></li> tags, like this:

<span >
<h1>Customer 1</h1>
<ul>
<li>Exercise Physiologist</li>
<li>PT, DPT, COMT</li>
<li>Class Name</li>
<li>South Physical Therapy</li>
<li>City 1</li>
<li>AR</li>
</ul>
</span>

I also need to account for empty items, i.e. if the "Class Name" doesn't exits, skip outputting the <li></li> for that item.

I've looked at this question Generate a set of HTML list items from a comma separated list? PHP but I get the error explode(): Argument #2 ($string) must be of type string, array given.

How can I explode/implode to output the needed html?

Or is there another php function more useful?

CodePudding user response:

Because your cell values use \n as a delimiter, first use the explode function to explode the cell value from a string to an array. Then, take the first item of this array to add as the header.

The first parameter of the implode function can be used for a separator. You can pass all but the first entry of your array into this function using the array_slice function. If you want your array items to display as an unordered list, you can use "</li><li>" as your separator. To skip empty list items you can use str_replace to replace all instances of "<li></li>".

foreach($values as $val) {
    $valuearray = explode("\n", implode($val)); // explode string into array
    $valuespan = "<span class=\"record\">";
    $valuespan = $valuespan . "<h1>" . $valuearray[0] . "</h1>"; // add heading
    $valuespan = $valuespan . "<ul><li>" . implode("</li><li>", array_slice($valuearray, 1)) . "</li></ul></span>"; // add remaining array as list items
    $valuespan = str_replace("<li></li>", "", $valuespan); // remove empty list items
    echo $valuespan;
}
  • Related