2

以下是在获取我想要的数据方面运行良好的代码块。不要笑,这可能效率低下,但我正在学习:) 我想要的是使用 $totalLength 变量,当 $totalLength 为 1500 字节/字符时停止收集数据(理想情况下,以完整的结尾字,但我不是在寻找奇迹!)。无论如何,代码:

$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 很重要,但是当我尝试在其中放置一个 while 语句时,它就挂了。我尝试在 for 之前和之后放置 while 语句,但没有任何乐趣。我做错了什么以及如何最好地解决这个问题?

仅供参考 $descriptionValue,是使用 DOM 和 xpath 从 HTML 解析的数据,我尝试的是 while($totalLength <= 1500)

4

3 回答 3

3

也许这就是你想要的:

if ($totalLength > 1500) {
    break;
}
于 2012-10-31T12:05:45.590 回答
1

for只需在循环中放置一个条件。一旦条件评估为真,它将跳出循环。

// for () { ...
if ($totalLength > 1500) {
    break;
}
// }

基本上,break结束当前 for、foreach、while、do-while 或 switch 结构的执行。您可以在手册中找到有关 PHP 控制结构的更多信息。

于 2012-10-31T12:06:04.133 回答
0

您也可以删除 for 并添加

$k = 0;    
while ($totalLength <= 1500 || $k < $descriptionValue->length)

并在循环内增加值 ok $k

于 2012-10-31T12:07:46.617 回答