Skip to content
Advertisement

replace li tag with different tag according to the bullet level

need to replace the li tags with different customized tag according to the bullet level, For the li level1 keep it as is, for level2 change li to lii, for level3 change li to liii

For example for the input below (I have only 2 bullet levels in the below code just as example)

<p><strong>Basics to Macros & VBA Code</strong></p>
<ul>
<li>Create a Macro MsgBox
<ul>
<li>test</li>
<li>test 1</li>
</ul>
</li>
<li>Workbook and Worksheet</li>
<li>Object Range</li>
<li>Object Variables</li>
<li>If Then Statement</li>
<li>Loop</li>
<li>Macro Errors</li>
<li>String Manipulation</li>
<li>Date and Time</li>
<li>Events</li>
<li>Array</li>
<li>Function and Sub</li>
<li>Application Object</li>
<li>Final Project</li>
</ul>

desired output

<p><strong>Basics to Macros & VBA Code</strong></p>
<ul>
<li>Create a Macro MsgBox
<ul>
<lii>test</lii>
<lii>test 1</lii>
</ul>
</li>
<li>Workbook and Worksheet</li>
<li>Object Range</li>
<li>Object Variables</li>
<li>If Then Statement</li>
<li>Loop</li>
<li>Macro Errors</li>
<li>String Manipulation</li>
<li>Date and Time</li>
<li>Events</li>
<li>Array</li>
<li>Function and Sub</li>
<li>Application Object</li>
<li>Final Project</li>
</ul>

as you can see the code should change only 2 lines (as it’s level 2) and keep li level1 as is

<lii>test</lii>
<lii>test 1</lii>

my code in php

function clean($str)
{   
    $level = 0;
    $str_temp = $str;
    //$pos = strpos($str,"<ul>");
    $str1 = "";
    foreach(preg_split("/((r?n)|(rn?))/", $str_temp) as $line){
        $line1 = "";
        if(strpos($line,"<ul>")){
            $level++;
        }elseif(strpos($line,"</ul>")){
            $level--;
        }
                
        if(level == 2){
            $str1 .= str_replace("<li>", "<lii>", $line1);
        }elseif(level == 3){
            $str1 .= str_replace("<li>", "<liii>", $line1);
        }else{
            $str1 .= $line1;
        }
    } 

    $str1 = trim($str1);
    return $str1;
}

Update

1- the html code is created by tinymce and stored in DB 2- I am using FPDF to output the DB to PDF file 3- I am using a specific library to FPDF to parse the html tags to have a specific style for each tag

Instead of devoting the questions, it’s better if you try to help if you can, and keep it as is if you don’t have anything to provide.

Advertisement

Answer

I reviewed my code and it works fine now, for the FPDF – witetag library purpose I removed the UL tags and closed each li tag at the end of the line, kept other tags as is.

function clean($str)
{   
    $str1 = "";
    $level = 0;
    foreach(preg_split("/((r?n)|(rn?))/", $str) as $line){
        if(strpos($line,"<ul>") !== false ){
            $level++;
        }elseif(strpos($line,"</ul>")!== false){
            $level--;
        }
        
        if(strpos($line,"<li>")!== false){
            if(strpos($line,"</li>")=== false){
                $line = $line . "</li>";
            }
        }elseif(strpos($line,"</li>")!== false){
            $line = str_replace("</li>", "", $line);
        }else{
            $line= $line;
        }
        
        if($level == 2){
            $line = str_replace("<li>", "<lii>", $line);
            $line = str_replace("</li>", "</lii>", $line);
            $str1 .=  $line;
        }elseif($level == 3){
            $line = str_replace("<li>", "<liii>", $line);
            $line = str_replace("</li>", "</liii>", $line);
            $str1 .=  $line;
        }else{
            $str1 .= $line;
        }
        

    } 

    $str1 = str_replace("<ul>", "", $str1);
    $str1 = str_replace("</ul>", "", $str1);

    $str1 = trim($str1);
    return $str1;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement