title: php while inside for loop, using strlen
body: <p>The following is a code block that's working fine as regards getting the data I want.
  Don't laugh, it's probably inefficient, but I'm learning :)
  What I want, is to use the $totalLength variable, to stop gathering data when the $totalLength is, say 1500 bytes/characters (ideally, ending on a full word, but I'm not looking for miracles!). Anyway, the code:</p>
  
  <pre><code>$paraLength = 0;
  $totalLength = 0;
  for ($k = 0; $k &lt; $descriptionValue-&gt;length; $k++) {        //define integer k as 0, get every description using ($k = 0; $k &lt; $descriptionValue-&gt;length; $k++), increment the k loop (to get only 14 elements, use ($k &lt;= 13))
  $totalLength = $totalLength + $paraLength;
  echo $totalLength." Total&lt;br /&gt;";
  $descNode = $descriptionValue-&gt;item($k)-&gt;nodeValue;       //find each description element
  $descNode = trim($descNode);        //trim any whitespace around the element
  $descPara = strip_tags($descNode);        //remove any HTML tags from the elements
  $paraLength = (strlen($descPara));        //find the length of each element
  //if (preg_match('/^([0-9 ]+)$/', $descPara)) {       //if element starts with numbers followed by a space, define it as a telephone number
  //    $number = $descPara;
  //    fwrite ($fh, "\t\t".'&lt;div id="tel"&gt;'.$number."&lt;/div&gt;\n");        //write a div with id tel, containing the number                                                                      
  //}
  //else
  if (preg_match('/[A-Z]{4,}/', $descPara)) {        //if element starts with at least 4 uppercase characters, define it as a heading
      $heading = $descPara;
      $heading=ucfirst(strtolower($heading));       //convert the uppercase string to proper    
      fwrite ($fh, "\t\t".'&lt;div id="heading"&gt;&lt;h4&gt;'.$heading."&lt;/h4&gt;&lt;/div&gt;\n");        //write a div with id heading, containing the heading in h4 tags                                                                     
  }
  else if (preg_match('/\d*\.\d{1,}[m x]/', $descPara)) {       //if the element contains any number of digits followed by a dot, at least one further digit and the letters m x, define it as a heading based on it containing room measurements (this pattern matches at least two number after the dot \d*{2,}}
      $room = $descPara;
      fwrite ($fh, "\t\t".'&lt;div id="roomheading"&gt;&lt;h4&gt;'.$room."&lt;/h4&gt;&lt;/div&gt;\n");       //write a div with id roomheading, containing the heading in h4 tags
  }
  else if (preg_match('/^Disclaimer/i', $descPara)) {        //if the element contains the word Disclaimer, define it as such
      $disclaimer = $descPara;
      fwrite ($fh, "\t\t".'&lt;div id="disclaimer"&gt;&lt;h4&gt;'.$disclaimer."&lt;/h4&gt;&lt;/div&gt;\n");        //write a div with id disclaimer, containing the heading in h4 tags
  }
  else if (strlen($paraLength&lt;14 &amp;&amp; $paraLength&gt;3)) {       //when all else fails, if the element is less than 14 but more than 3 characters, also define it as a heading
      $other = $descPara;
      fwrite ($fh, "\t\t".'&lt;div id="other"&gt;&lt;h4&gt;'.$other."&lt;/h4&gt;&lt;/div&gt;\n");        //write a div with id other and the heading in h4 tags
  }
  else {
  fwrite ($fh, "\t\t\t&lt;p&gt;".$descPara."&lt;/p&gt;\n");       //anything else is considered content, so write it out inside p tags
  }
  }
  </code></pre>
  
  <p>$totalLength counts nicely, but when I tried to put a while statement in there, it just hung. I tried putting the while statement before and after the for, but no joy. What am I doing wrong and how best to solve this one?</p>
  
  <p>FYI $descriptionValue, is data parsed from HTML using DOM &amp; xpath, the while I tried was while($totalLength &lt;= 1500)</p>