Jump to content

formatting asterisks to lists


The Little Guy

Recommended Posts

I have Items in a database with items that look like this:

 

This is a paragraph it isn't apart of the lists below.
* list one item.
* list one item.
* list one item.
* list one item.
* list one item.

* list two item.
* list two item.
* list two item.
* list two item.
This is another paragraph it isn't apart of the lists above.

 

I would like to convert it to an un-ordered list. The list items will always be formatted:

beginning of line asterisk space string (newline or end of line)

 

as you can see there is a gap between the two lists, and I am not sure of the best way to do this...

 

so basically the final result should look like this:

 

<ul>
<li>list one item.</li>
<li>list one item.</li>
<li>list one item.</li>
<li>list one item.</li>
</ul>
<ul>
<li>list two item.</li>
<li>list two item.</li>
<li>list two item.</li>
<li>list two item.</li>
</ul>

Link to comment
https://forums.phpfreaks.com/topic/188300-formatting-asterisks-to-lists/
Share on other sites

hi,

 

my first thought was a explode() the whole string based on the \n

 

after that you loop through the items.

If the string starts (substr()) with "* ", you replace it with <li> and close the tag after the string.

 

finally, you'd have to build in a system to add the <ul> and </ul> tags.

best thing would be to add the <ul> when the loop encounters the first "* ",

add a closing and new opening tag when the loop encounters a blank and close the tag at the end.

 

maybe there are smarter ways to get this done...

<?php

$string = "This is a paragraph it isn't apart of the lists below.
* list one item.
* list one item.
* list one item.
* list one item.
* list one item.

* list two item.
* list two item.
* list two item.
* list two item.

This is another paragraph it isn't apart of the lists above.";

function makeList($string) {
    $string = explode("<br />", nl2br($string)); // Could probably just use \n but I wasn't sure.
    $last = false;
    $list = "";
    foreach($string as $line) {
        $line = trim($line);
        if(substr($line, 0, 2) == "* ") {
            if($last == "ul") {
                $list .= "<li>".substr($line, 2)."</li>\n";
            } else {
                $list .= "<ul>\n";
                $list .= "<li>".substr($line, 2)."</li>\n";
                $last = "ul";
            }
        }
        if(trim($line) == "") {
            $list .= "</ul>\n";
            $last = false;
        }
    }
    if(trim(substr($list, -6)) != "</ul>") $list .= "</ul>";
    return $list;
}

echo makeList($string);

?>

 

Wrote that just for you :P Wanted a bit of a challenge.

<?php

$string = "This is a paragraph it isn't apart of the lists below.
* list one item.
* list one item.
* list one item.
* list one item.
* list one item.

* list two item.
* list two item.
* list two item.
* list two item.

This is another paragraph it isn't apart of the lists above.";

function makeList($string) {
    $string = explode("<br />", nl2br($string)); // Could probably just use \n but I wasn't sure.
    $last = false;
    $list = "";
    foreach($string as $line) {
        $line = trim($line);
        if(substr($line, 0, 2) == "* ") {
            if($last == "ul") {
                $list .= "<li>".substr($line, 2)."</li>\n";
            } else {
                $list .= "<ul>\n";
                $list .= "<li>".substr($line, 2)."</li>\n";
                $last = "ul";
            }
        }
        if(trim($line) == "") {
            $list .= "</ul>\n";
            $last = false;
        }
    }
    if(trim(substr($list, -6)) != "</ul>") $list .= "</ul>";
    return $list;
}

echo makeList($string);

?>

 

Wrote that just for you :P Wanted a bit of a challenge.

 

Awesome! thanks!

 

I did have to add two lines though, so it would work with what I already had:

 

function formatPage($string){
    $string = explode("\n", $string);
    $last = false;
    $list = "";
    foreach($string as $line){
        $line = trim($line);
        if(substr($line, 0, 2) == "* "){
            if($last == "ul"){
                $list .= "<li>".substr($line, 2)."</li>\n";
            }else{
                $list .= "<ul>\n";
                $list .= "<li>".substr($line, 2)."</li>\n";
                $last = "ul";
            }
        }else{// begin addition
	$list .= '<p>'.$line.'</p>';
}// end addition
        if(trim($line) == ""){
            $list .= "</ul>\n";
            $last = false;
        }
    }
    if(trim(substr($list, -6)) != "</ul>") $list .= "</ul>";
    return $list;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.