1

Topic: PHP warning on str_repeat in hl_tidy

Hi Patanaik,
regarding the above warning I would like to suggest the following change:

--- htmLawed/htmLawed.php    2012/07/27 10:17:06    39958
+++ htmLawed/htmLawed.php    2012/07/31 11:06:55    39984
@@ -664,11 +664,11 @@
  $y = !$x ? ltrim($e, '/') : ($x > 0 ? substr($e, 0, strcspn($e, ' ')) : 0);
  $e = "<$e>";
  if(isset($d[$y])){
-  if(!$x){echo "\n", str_repeat($s, --$n), "$e\n", str_repeat($s, $n);}
-  else{echo "\n", str_repeat($s, $n), "$e\n", str_repeat($s, ($x != 1 ? ++$n : $n));}
+  if(!$x){echo "\n", str_repeat($s, (--$n>=0?$n:0)), "$e\n", str_repeat($s, ($n>=0?$n:0));}
+  else{echo "\n", str_repeat($s, ($n>=0?$n:0)), "$e\n", str_repeat($s, ($x != 1 ? (++$n>=0?$n:0) : ($n>=0?$n:0)));}
   echo ltrim($r); continue;
  }
- $f = "\n". str_repeat($s, $n);
+ $f = "\n". str_repeat($s, ($n>=0?$n:0));
  if(isset($c[$y])){
   if(!$x){echo $e, $f, ltrim($r);}
   else{echo $f, $e, $r;}

2

Re: PHP warning on str_repeat in hl_tidy

I have not encountered a PHP warning with this part of htmLawed. Can you post an example input? Thanks (and for the other posts).

3

Re: PHP warning on str_repeat in hl_tidy

It happens occasionally in our production enviroment running php5.4.4
I have seen it on my developing machine as well running php5.3.8
I will post an example, as soon as I get a hold of one.

4

Re: PHP warning on str_repeat in hl_tidy

OK, I think this happens with some types of inputs if config. parameter 'balance' is turned off while 'tidy' is turned on. An example input:

</span><hr /></div></div></span><hr /></div></div></span><hr /></div></div>

I think the following fix is good. It's similar to what you suggest but involves only one line (in function hl_tidy).

// find line
if(!$x){echo "\n", str_repeat($s, --$n), "$e\n", str_repeat($s, $n);}

// replace with line
if(!$x){echo "\n", str_repeat($s, (!$n ? 0 : --$n)), "$e\n", str_repeat($s, $n);}

5

Re: PHP warning on str_repeat in hl_tidy

With the above fix, with config. parameter 'balance' set to 0 (disabled), the 'tidying' may not be perfect for a portion of the input. The following may be a better code modification:

// change variable named $n to $no on line 650, for function hl_tidy(), in htmLawed.php (version 1.1.13)

// replace lines 655-677, for function hl_tidy(), in htmLawed.php (version 1.1.13), with this block of lines

$to = explode('<', $t);
$do = 1;
while($do){
 $n = $no;
 $t = $to;
 ob_start();
 if(isset($d[$p])){echo str_repeat($s, ++$n);}
 echo ltrim(array_shift($t));
 for($i=-1, $j=count($t); ++$i<$j;){
  $r = ''; list($e, $r) = explode('>', $t[$i]);
  $x = $e[0] == '/' ? 0 : (substr($e, -1) == '/' ? 1 : ($e[0] != '!' ? 2 : -1));
  $y = !$x ? ltrim($e, '/') : ($x > 0 ? substr($e, 0, strcspn($e, ' ')) : 0);
  $e = "<$e>"; 
  if(isset($d[$y])){
   if(!$x){
    if($n){echo "\n", str_repeat($s, --$n), "$e\n", str_repeat($s, $n);}
    else{++$no; ob_end_clean(); continue 2;}
   }
   else{echo "\n", str_repeat($s, $n), "$e\n", str_repeat($s, ($x != 1 ? ++$n : $n));}
   echo ltrim($r); continue;
  }
  $f = "\n". str_repeat($s, $n);
  if(isset($c[$y])){
   if(!$x){echo $e, $f, ltrim($r);}
   else{echo $f, $e, $r;}
  }elseif(isset($b[$y])){echo $f, $e, $r;
  }elseif(isset($a[$y])){echo $e, $f, ltrim($r);
  }elseif(!$y){echo $f, $e, $f, ltrim($r);
  }else{echo $e, $r;}
 }
 $do = 0;
}

With this modification, $n never 'natively' reaches 0 and incremental indentation is maintained. With the modification in my earlier post, it can reach 0, when incremental indentation fails.

6

Re: PHP warning on str_repeat in hl_tidy

Thanks Patnaik, for your effort and quick reply.
I will test the latter.

7 (edited by leithoff 2012-08-02 05:45:19)

Re: PHP warning on str_repeat in hl_tidy

Sadly the latter approach leads to an infinte loop, if I test it on code like that.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 9.00.8112.16447">
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>Bin eigentlich gar nicht in Babbellaune,... muß aber bißle was 
erzählen.</DIV>
<DIV>&nbsp;</DIV>
<UL>
  <LI>Das Bild zeigte wirklich 10, Downing Street; und die Info zu dem 
  Ausstellungraum fand ich interessant 
  <DIV id=ContentFirst class=ContentFlow>
  <DIV class="fullInterviewItem interviewItem">
  <DIV>
  <P><FONT color=#000080 face="Times New Roman">Yan Lei arbeitet in seinem 
  "Limited Art Project" mit dem Volkswagenwerk zusammen. Im Werk in Baunatal hat 
  er mit Hilfe von Assistenten insgesamt 360 Bilder gemalt, die nun in der 
  documenta-Halle in einem Gemäldeschrank präsentiert werden. Sie hängen von der 
  Decke und an den Wänden, sodass der gesamte Raum mit Gemälden gefüllt ist. 
  <BR><BR>Im Verlauf der Schau werden die Gemälde nach und nach im VW-Werk mit 
  Autolack übermalt, kehren wieder in die documenta-Halle zurück und verwandeln 
  den Raum in eine Ausstellung monochromer Bilder. Das Projekt thematisiert die 
  Allmacht der Bilder im digitalen Zeitalter. <FONT color=#000000 face=Arial>Da 
  bin ich erfreut, daß wir zur Halbzeit dort waren 
  ;-)</FONT></FONT></P></DIV></DIV></DIV></LI>
  <LI>
  <DIV class=ContentFlow>
  <DIV class="fullInterviewItem interviewItem">
  <DIV>
  <P>juhu, heute Nacht sind meine Minusstunden verfallen. Und heute gleich mit 
  49 Minusminuten gestartet..... BLÖD! Aber Samstag Nachmittag wird mit 
  Sicherheit ein Plus vor der Zahl stehen.</P></DIV></DIV></DIV></LI>
  <LI>
  <DIV class="fullInterviewItem interviewItem">
  <DIV>
  <P>gestern abend zufällig HR geguckt. "Herrliches Hessen", Thema Darmstadt. 
  Und da fiel mir ein, daß ich schon ewig mal die Mathildenhöhe besuchen wollte. 
  Außerdem sprachen die davon, daß Darmstadt 10 (?), 15 (?) tolle Parkanlagen 
  hätte. Ausflugsvorschlag?!</P></DIV></DIV></LI>
  <LI>
  <DIV class="fullInterviewItem interviewItem">
  <DIV>
  <P>nächste Woche haben sie das Thema "Fulda"..... und da waren wir ja beide 
  noch nicht. Wenn ich daran denken sollte, gucke ich mir das mal an. Ach so, 
  falls das Sporteln nicht länger dauern sollte.</P></DIV></DIV></LI>
  <LI>
  <DIV class="fullInterviewItem interviewItem">
  <DIV>
  <P>wann im August hast Du eigentlich Urlaub? Vermutlich in der 
  "Familienwoche"? Die war nächste Woche, oder? Lust auf Darmstadt? Ab frühem 
  Mittag?</P></DIV></DIV></LI>
  <LI>
  <DIV class="fullInterviewItem interviewItem">
  <DIV>
  <P>ich werde verrückt - ist es schon Verfolgungswahn?? ;-))&nbsp; . Gerade im 
  Moment führt Hans Eichel über die Documenta (wieder 
  HR).....</P></DIV></DIV></LI></UL>
<DIV class="fullInterviewItem interviewItem">
<DIV>
<P>Badezimmerplanung soweit unter Kontrolle, Omi´s Termine abgearbeitet, die 
Woche wird langsam übersichtlicher ;-))</P>
<P>&nbsp;</P>
<P>Schönen Restabend</P>
<P>Miriam</P></DIV></DIV>
<DIV class="fullInterviewItem interviewItem">
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</P></DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #000000 2px solid; PADDING-LEFT: 5px; PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px">
  <DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV 
  style="FONT: 10pt arial; BACKGROUND: #e4e4e4; font-color: black"><B>From:</B> 
  <A title=you@me.de href="mailto:you@me.de">Klaus Leithoff</A> 
  </DIV>
  <DIV style="FONT: 10pt arial"><B>To:</B> <A title=me@you.de 
  href="mailto:me@you.de">Miriam xyz</A> </DIV>
  <DIV style="FONT: 10pt arial"><B>Sent:</B> Sunday, July 29, 2012 2:38 PM</DIV>
  <DIV style="FONT: 10pt arial"><B>Subject:</B> Re: Angie, Hannelore,....</DIV>
  <DIV><BR></DIV>Doch&nbsp; - zu meiner Schande - genau dort.<BR>&nbsp; 
  <P>----------------ursprüngliche Nachricht----------------- <BR>Von: "Miriam 
  xyz" &lt;<A href="mailto:me@you.de">me@you.de</A>&gt; 
  <BR>An: "Klaus Leithoff" &lt;<A 
  href="mailto:you@me.de">you@me.de</A>&gt; <BR>Datum: Sun, 29 
  Jul 2012 10:54:50 +0200 
  <BR>----------------------------------------------------------</P>
  <BLOCKQUOTE type="cite"><BR> 
    <DIV>Cola getankt?</DIV>
    <DIV>Aber doch nicht etwa beim mittags noch belächelten Coffee to go und 
    Burgeranbieter?&nbsp; :-)</DIV>
    <DIV>&nbsp;</DIV>
    <DIV>Habe das Gefühl, vom Thema Documenta verfolgt zu werden. Am Freitag 
    waren die groß in der Hessenschau (Halbzeit der Ausstellung), und gestern 
    war Documentawerbung auf dem Einkauf Aktuell.</DIV>
    <BLOCKQUOTE 
    style="PADDING-LEFT: 5px; PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; 000000: ">
      <DIV>----- Original Message -----</DIV>
      <DIV><B>From:</B> <A href="mailto:you@me.de">Klaus 
      Leithoff</A></DIV>
      <DIV><B>To:</B> <A href="mailto:me@you.de">Miriam 
xyz</A></DIV>
      <DIV><B>Sent:</B> Saturday, July 28, 2012 3:21 PM</DIV>
      <DIV><B>Subject:</B> Re: Angie, Hannelore,....</DIV>
      <DIV>&nbsp;</DIV>Hi Miriam,<BR>na, ich bin gut heimgekommen. Bin aber bei 
      Bischofsheim noch mal eine Cola tanken gefahren.<BR>Irgendwie war ich 
      durstig,<BR>Ärger mit dem Magen/Darm: blöd.<BR><SPAN>Telefon (mobil): 
      0175-6225460</SPAN>, (ist aber trotzdem meistens aus, das hat sich nicht 
      geändert.)<BR>Ciao<BR>Klausi<BR>&nbsp; 
      <P>----------------ursprüngliche Nachricht----------------- <BR>Von: 
      "Miriam xyz" &lt;me@you.de&gt; <BR>An: "Leithoff, Klaus" 
      &lt;you@me.de&gt; <BR>Datum: Fri, 27 Jul 2012 16:33:31 +0200 
      <BR>----------------------------------------------------------</P>
      <BLOCKQUOTE type="cite"><BR>
        <DIV>Hi Klausi,</DIV>
        <DIV>&nbsp;</DIV>
        <DIV>na, wie war die Heimfahrt?</DIV>
        <DIV>&nbsp;</DIV>
        <DIV>Hätte ja wetten können, daß ich schlafe wie ein Stein. Aber 
        Pustekuchen. Erst nervte noch der Magen-Darm-Trakt, dann nachts 3 Mal 
        wach geworden (einfach so), und morgens wieder Ärger mit Magen/Darm..... 
        aber jetzt scheint alles wieder okay.</DIV>
        <DIV>Zustellung war soweit okay, nur halt etwas zu heiß da 
draußen.</DIV>
        <DIV>&nbsp;</DIV>
        <DIV>Hier der Link zur "Doris-Zitronenpresse". Ach nee, ist ja doch die 
        Angie ;-))</DIV>
        <DIV>&nbsp;</DIV>
        <DIV><A 
        href="http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM">http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM</A></DIV>
        <DIV>&nbsp;</DIV>
        <DIV>Und die Hannelore:</DIV>
        <DIV><A 
        href="http://de.wikipedia.org/wiki/Hannelore_Elsner">http://de.wikipedia.org/wiki/Hannelore_Elsner</A></DIV>
        <DIV>&nbsp;</DIV>
        <DIV>Die Dame von den Freunden und Förderern der Fasanerie hat zackig 
        geantwortet; alles geklärt. Schön so. Werde mein Konto beobachten 
        ;-)</DIV>
        <DIV>&nbsp;</DIV>
        <DIV>Hey, da fehlt noch Deine neue Mobilnummer.</DIV>
        <DIV>&nbsp;</DIV>
        <DIV>So, mal kurz die wasserliebenden Pflanzen gießen und gut für 
        heute.</DIV>
        <DIV>&nbsp;</DIV>
        <DIV>Schönen Abend / schönes Wochenende</DIV>
        <DIV>Miriam</DIV>
        <DIV>&nbsp;</DIV></BLOCKQUOTE><BR></BLOCKQUOTE></BLOCKQUOTE><BR></BLOCKQUOTE></BODY></HTML>

8

Re: PHP warning on str_repeat in hl_tidy

Thanks for the feedback. I had not tested the modification with the 'while' loop. Will look into it. Should be something minor.

9

Re: PHP warning on str_repeat in hl_tidy

No problem, that is the least I could do to help.

10

Re: PHP warning on str_repeat in hl_tidy

I have made a correction in the code I posted above.

I tried the input you posted, the one leading to an infinite loop, and I do not see any issue, but I did not encounter the infinite looping with my earlier code either. I have tested values of both 0 and 1 for config. parameter 'balance' in combination with values of -1, 0, '1t1' anc '1t2' for 'tidy' (all other config. values are default values).

11 (edited by leithoff 2012-08-03 04:53:01)

Re: PHP warning on str_repeat in hl_tidy

Then I must have done something wrong while preparing the example HTML.
I will try to correct that one, ... I know now better what happened:

$htmLawed_config = array('comment'=>1, //remove comments
                'keep_bad'=>6,
                'balance'=>0,//turn off tag-balancing (config['balance']=>0). That will not introduce any security risk; only standards-compliant tag nesting check/filtering will be turned off (basic tag-balance will remain; i.e., there won't be any unclosed tag, etc., after filtering)
                'tidy'=>1,
                'elements' => "* -script",
                'schemes'=>'href: file, ftp, http, https, mailto; src: cid, data, file, ftp, http, https; *:file, http, https',
                'hook_tag' =>"hl_email_tag_transform",
            );

Source:

<HTML>
<BODY bgColor=#ffffff>
<DIV>Hi Klausi,</DIV>
<DIV>&nbsp;</DIV>
<DIV>na, wie war die Heimfahrt?</DIV>
<DIV>&nbsp;</DIV>
<DIV>Hätte ja wetten können, daß ich schlafe wie ein Stein. Aber Pustekuchen. 
Erst nervte noch der Magen-Darm-Trakt, dann nachts 3 Mal wach geworden (einfach 
so), und morgens wieder Ärger mit Magen/Darm..... aber jetzt scheint alles 
wieder okay.</DIV>
<DIV>Zustellung war soweit okay, nur halt etwas zu heiß da draußen.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Hier der Link zur "Doris-Zitronenpresse". Ach nee, ist ja doch die Angie 
;-))</DIV>
<DIV>&nbsp;</DIV>
<DIV><A 
href="http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM">http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM</A></DIV>
<DIV>&nbsp;</DIV>
<DIV>Und die Hannelore:</DIV>
<DIV><A 
href="http://de.wikipedia.org/wiki/Hannelore_Elsner">http://de.wikipedia.org/wiki/Hannelore_Elsner</A></DIV>
<DIV>&nbsp;</DIV>
<DIV>Die Dame von den Freunden und Förderern der Fasanerie hat zackig 
geantwortet; alles geklärt. Schön so. Werde mein Konto beobachten ;-)</DIV>
<DIV>&nbsp;</DIV>
<DIV>Hey, da fehlt noch Deine neue Mobilnummer.</DIV>
<DIV>&nbsp;</DIV>
<DIV>So, mal kurz die wasserliebenden Pflanzen gießen und gut für heute.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Schönen Abend / schönes Wochenende</DIV>
<DIV>Miriam</DIV>
<DIV>&nbsp;</DIV></BODY></HTML>

resulted in the output of:

<div>
 Hi Klausi,
</div>
&nbsp;div>Hi Klausi,
</div>
&nbsp;/div> &nbsp;
 </div>
 na, wie war die Heimfahrt?
</div>
&nbsp;/div> na, wie war die Heimfahrt?
  </div>
  &nbsp;
 </div>
 Hätte ja wetten können, daß ich schlafe wie ein Stein. Aber Pustekuchen. Erst nervte noch der Magen-Darm-Trakt, dann nachts 3 Mal wach geworden (einfach so), und morgens wieder Ärger mit Magen/Darm..... aber jetzt scheint alles wieder okay.
</div>
Zustellung war soweit okay, nur halt etwas zu heiß da draußen./div> &nbsp;
   </div>
   Hätte ja wetten können, daß ich schlafe wie ein Stein. Aber Pustekuchen. Erst nervte noch der Magen-Darm-Trakt, dann nachts 3 Mal wach geworden (einfach so), und morgens wieder Ärger mit Magen/Darm..... aber jetzt scheint alles wieder okay.
  </div>
  Zustellung war soweit okay, nur halt etwas zu heiß da draußen.
 </div>
 &nbsp;
</div>
Hier der Link zur "Doris-Zitronenpresse". Ach nee, ist ja doch die Angie ;-))/div> Hätte ja wetten können, daß ich schlafe wie ein Stein. Aber Pustekuchen. Erst nervte noch der Magen-Darm-Trakt, dann nachts 3 Mal wach geworden (einfach so), und morgens wieder Ärger mit Magen/Darm..... aber jetzt scheint alles wieder okay.
    </div>
    Zustellung war soweit okay, nur halt etwas zu heiß da draußen.
   </div>
   &nbsp;
  </div>
  Hier der Link zur "Doris-Zitronenpresse". Ach nee, ist ja doch die Angie ;-))
 </div>
 &nbsp;
</div>
<a href="http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM">http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM</a>/div> Zustellung war soweit okay, nur halt etwas zu heiß da draußen.
     </div>
     &nbsp;
    </div>
    Hier der Link zur "Doris-Zitronenpresse". Ach nee, ist ja doch die Angie ;-))
   </div>
   &nbsp;
  </div>
  <a href="http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM">http://www.amazon.de/Dinamo-ANGIE-Zitronenpresse/dp/B000W4QZVM</a>
 </div>
 &nbsp; Und die Hannelore: <a href="http://de.wikipedia.org/wiki/Hannelore_Elsner">http://de.wikipedia.org/wiki/Hannelore_Elsner</a>
</div>
&nbsp; Die Dame von den Freunden und Förderern der Fasanerie hat zackig geantwortet; alles geklärt. Schön so. Werde mein Konto beobachten ;-) &nbsp; Hey, da fehlt noch Deine neue Mobilnummer. &nbsp; So, mal kurz die wasserliebenden Pflanzen gießen und gut für heute. &nbsp; Schönen Abend / schönes Wochenende Miriam &nbsp; 

And my webapplication choked on it, never returning on the Ajax request. Sorry for misleading. But with the changes applied, output is now as expected and it works like a charm with the original of the example.

12

Re: PHP warning on str_repeat in hl_tidy

Good. Again, thanks for the feedback.

13

Re: PHP warning on str_repeat in hl_tidy

The new release of htmLawed (1.1.14, 8 Aug'12) includes this code modification.