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

294 Views Asked by At

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 class="record">Customer 1 \n
Exercise Physiologist \n
PT, DPT, COMT \n
Class Name \n
South Physical Therapy \n
City 1 \n
AR \n
</span>


<span class="record">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 class="record">
<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?


Edit 12/13/21

I found I needed to be able to use if constructs for some fields, and that didn't work with concatenating the string, so this is what I'm using. It may be ugly in a programmatical sense; if so, add comments.

foreach($values as $val) {
$valuearray =explode("\n", implode($val)); // explode string into array
    
$name = $valuearray[0];
$focus = $valuearray[1];
$certification = $valuearray[2];
$course = $valuearray[3];
$business = $valuearray[4];
$url = $valuearray[5];
$address = $valuearray[6];
$city = $valuearray[7];
$state = $valuearray[8];

echo '<h1 class="name">' . $name . '</h1>';
echo '<ul>';

if($focus ?? true) {echo '<li class="focus">Focus: ' . $focus . '</li>'; }

if($certification ?? true) {echo '<li class="certification">Certification: ' . $certification . '</li>'; }

if($course ?? true) {echo '<li class="course">Course: ' . $course . '</li>'; }

if($url ?? true) {
echo '<li class="business">Business: <a class="business-link" target="_blank" rel="noopener noreferrer" href="https://' . $url . ' ">' . $business . '</a></li>';
} elseif ($business ?? true) {
echo '<li class="business">Business: ' . $business . '</li>';
}

if($address ?? true) {
echo '<li class="address">Location: ' . $address . ', ' . $city . ', ' . $state . '</li>';
} else {
echo '<li class="address">Location: ' . $city . ', ' . $state . '</li>';
}

echo '</ul>';
}
1

There are 1 best solutions below

6
George Sun On BEST ANSWER

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