Jump to content

[SOLVED] Explode to <ul><li>


conker87

Recommended Posts

As the title suggests. I want to explode a string:

 

$string = "one, two, three"

 

As an example, into something like this:

<ul><li>one</li><li>two</li><li>three</li></ul>

 

I've looked around google, but I know you guys are pretty good. Any ideas?

Link to comment
Share on other sites

<?php
function explode_li($string) {
      $array = explode("</li>", $string);
      foreach ($array as $key => $val) {
              $array[$key] = strip_tags($val);
      }
      array_pop($array); // last element should be nothing.
      return $array
}
?>

 

Give that a try. www.php.net/strip_tags www.php.net/explode

 

EDIT: Just noticed what the question was, a different version from poco is

 

<?php

$string = "one, two, three";
function explode_tolist($string) {
      $array = explode(",", $string);
      $string = "<ul>";
     foreach ($array as $val){
             $string .= "<li>" . trim($val) . "</li>";
     }
     $string = "</ul>";
}

$exploded = explode_tolist($string);

echo $exploded;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.