1

I'm creating a forum in which the user input text that he will be able to format using certain method. I've been able to accomplish most of the string work using substring() but I'm having trouble witht the Ordered List and Unordered list.

I'll take the unordered as an example.

The user input will be:

Example of text and here is my UL:
* Element 1
* Element 2
* Element 3
* Element 4
* Element 5

Thank you. Another one:
* Another 1
* Another 2

It will go in the Database like this and then I want to work this out in php to get the following output:

Example of text and here is my UL:
<ul>
    <li>Element 1</li>
    <li>Element 2</li> 
    <li>Element 3</li> 
    <li>Element 4</li> 
    <li>Element 5</li> 
<ul>

Thank you. Another one:
<ul>
    <li>Another 1</li>
    <li>Another 2</li> 
<ul>

The problem here is that i know how to replace the "*" for <li> but I can't figure out how to find the first 5 and then the 2 other one so they can both have their ul tags <ul></ul> around them.

I'm using bl2br() at the start of my function so the cariage are all

Here's a part of my function cutted down a bit to help:

function String_ToOutput($String_Output){

    //Replace Cariage with HTML Code
    $Temp_String = nl2br($String_Output);

    //List Code
    while(($pos = strpos($Temp_String, "\n*")) !== false){
        $Temp_String = substr($Temp_String, 0, $pos) . "<ul><li>" . substr($Temp_String, $pos + 3);
        $pos = strpos($Temp_String, "\n", $pos);
        while((substr($Temp_String, $pos+1, 1)) == "*"){
                $Next = strpos($Temp_String, "\n", $pos);
                $Temp_String = substr($Temp_String, 0, $pos) . "<li>" . substr($Temp_String, $pos + 2);
                $pos = $Next;
        }
    }


    return $Temp_String;
}

Thanks for your help

2
  • 1
    What is the definition - How is the author making clear that a new list starts? Just by a line not starting with * ? Commented Jun 26, 2015 at 8:34
  • Exactly. :) @KTAnj answer work exactly like this Commented Jun 26, 2015 at 19:13

1 Answer 1

1

Try this code.

function String_ToOutput($String_Output){

    //Replace Cariage with HTML Code
    $Temp_String = nl2br($String_Output);

    $lines=explode("\n",$Temp_String);

    $start_list=false;
    foreach($lines as &$line){ 
        if(strpos($line,'*')!==False){
            if(!$start_list)
                $line="<ul> ".$line;
            $line=str_replace('*',"<li>",$line)."</li>";
            $start_list=true;
        }
        else{

            if($start_list){
            $start_list=false;
            $line="</ul> ". $line;
            }
        }
        //echo $line;
    }
    $sring=implode("\n",$lines);
    return $sring;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! Work like a charm!
I have a question if you. How does Explode work? Does it separate the string in multiple part into an array separated by the specified string ("/n")?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.