php内部for循环,使用strlen

时间:2012-10-31 12:02:18

标签: php loops for-loop while-loop strlen

以下是一个在获取我想要的数据方面工作正常的代码块。 不要笑,这可能是低效的,但我正在学习:) 我想要的是使用$ 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语句,但没有快乐。我做错了什么以及如何最好地解决这个问题?

FYI $ descriptionValue,是使用DOM&amp; amp; xpath,我试过的时候是($ totalLength&lt; = 1500)

3 个答案:

答案 0 :(得分:3)

也许这就是你想要的:

if ($totalLength > 1500) {
    break;
}

答案 1 :(得分:1)

只需在for循环中添加条件即可。一旦条件评估为真,它就会跳出循环。

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

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

答案 2 :(得分:0)

您也可以删除for并添加

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

并在循环内部增加值ok $ k