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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

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.