mihaeu / html-formatter

HTML Formatter is a PHP library which pretty prints HTML.
17 stars 11 forks source link

Getting Error : Warning: str_repeat(): Second argument has to be greater than or equal to 0 #3

Closed trk closed 7 years ago

trk commented 8 years ago

Error line is : https://github.com/mihaeu/html-formatter/blob/master/src/HtmlFormatter.php#L48

Passed html :

require __DIR__.'/vendor/autoload.php';

echo Mihaeu\HtmlFormatter::format("<html lang='en' dir='ltr'><head><title>My Website</title></head><body class='homepage'><div class='container'><h1 class='h1-title'>H1 Title</h1><div class='body-content'>Body Content</div></div></body></html>");
// Result is
Warning:  str_repeat(): Second argument has to be greater than or equal to 0 : https://github.com/mihaeu/html-formatter/blob/master/src/HtmlFormatter.php#L48
<html lang='en' dir='ltr'>
<head>
    <title>
        My Website
    </title>
</head>
<body class='homepage'>
    <div class='container'>
        <h1 class='h1-title'>
            H1 Title
        </h1>
        <div class='body-content'>
            Body Content
        </div>
    </div>
</body>
</html>

if i add if else check for error location, no more error

if($indent > $indentWith || $indent == $indentWith) $lf = "\n".str_repeat($indentWith, $indent);
else $lf = "";

After my if else check and remove (html from $tagsWithoutIndentation) result is :

<html lang='en' dir='ltr'>
    <head>
        <title>
            My Website
        </title>
    </head>
    <body class='homepage'>
        <div class='container'>
            <h1 class='h1-title'>
                H1 Title
            </h1>
            <div class='body-content'>
                Body Content
            </div>
        </div>
    </body>
</html>

I am using your library functions for my module (HTML tag manager module) : https://github.com/trk/AvbMarkupHtml/blob/master/AvbMarkupHtml.module.php#L262

mihaeu commented 8 years ago

Hey @trk thanks so much for reporting this issue. I don't have time today, but I'll clean that up tomorrow.

Merry Christmas!

trk commented 8 years ago

On my side i updated your function again there are many repeat for str_repeat() function.

Here is my solution : https://github.com/trk/AvbMarkupHtml/blob/master/MarkupHtml.php#L597

Created a $indenter variable with a if else check and replaced with str_repeat() function.

$indenter = ($indent >= $indentWith) ? str_repeat($indentWith, $indent) : "";