Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:46:56+00:00 2026-06-13T15:46:56+00:00

The following is a code block that’s working fine as regards getting the data

  • 0

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:

$paraLength = 0;
$totalLength = 0;
for ($k = 0; $k < $descriptionValue->length; $k++) {        //define integer k as 0, get every description using ($k = 0; $k < $descriptionValue->length; $k++), increment the k loop (to get only 14 elements, use ($k <= 13))
$totalLength = $totalLength + $paraLength;
echo $totalLength." Total<br />";
$descNode = $descriptionValue->item($k)->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".'<div id="tel">'.$number."</div>\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".'<div id="heading"><h4>'.$heading."</h4></div>\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".'<div id="roomheading"><h4>'.$room."</h4></div>\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".'<div id="disclaimer"><h4>'.$disclaimer."</h4></div>\n");        //write a div with id disclaimer, containing the heading in h4 tags
}
else if (strlen($paraLength<14 && $paraLength>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".'<div id="other"><h4>'.$other."</h4></div>\n");        //write a div with id other and the heading in h4 tags
}
else {
fwrite ($fh, "\t\t\t<p>".$descPara."</p>\n");       //anything else is considered content, so write it out inside p tags
}
}

$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?

FYI $descriptionValue, is data parsed from HTML using DOM & xpath, the while I tried was while($totalLength <= 1500)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:46:57+00:00Added an answer on June 13, 2026 at 3:46 pm

    Maybe this is what You want:

    if ($totalLength > 1500) {
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following block of jQuery code that I'm using to copy some
I have the following code block that fails: this.redisClient.hmset('user:' + userObj.getUserId(), { 'userId' :
I've inherited a code block that contains the following regex and I'm trying to
The following code block runs without error but the 'data' is returned as what
Hi I have following code block public class Driver { static String x =
I have the following code block, as the content of my AppDelegate.h of an
I have the following code block for validation in PHP and I can't figure
I'm really stuck on why the following code block 1 result in output 1
Today I was met the following code block: #include <iostream> using namespace std; char
I need syntax help with the following code logic: I have a code block

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.