【问题标题】:php while inside for loop, using strlenphp while 在 for 循环中,使用 strlen
【发布时间】:2012-10-20 21:13:12
【问题描述】:

以下是在获取我想要的数据方面运行良好的代码块。 不要笑,这可能效率低下,但我正在学习:) 我想要的是使用 $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

【问题讨论】:

  • 如果你缩进你的代码会很好;-)
  • 你想在哪里放置一个while语句?在哪里?

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


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢你,这在一定程度上有效。正如我在对 Alvar 的回复中所提到的,这可能意味着结果远远超过 1500 个字符。我真的需要了解现代编程语言:D。我在 Sinclair Basic 中学习了 Gosub-Return、Goto 和 For-Next!等这个项目完成,我会好好学习的。
【解决方案2】:

你也可以删除for并添加

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

在循环内你增加值 ok $k

【讨论】:

  • 我喜欢这个声音,因为我想用 while 替换那个 for 循环,但是,我无法让你的建议起作用。我把你的建议代替了 for 语句和 $k++ 。请给我一个你认为它是如何工作的例子,好吗?
【解决方案3】:

也许这就是你想要的:

if ($totalLength > 1500) {
    break;
}

【讨论】:

  • 我喜欢这个解决方案,因为它意味着没有断字,但它可能意味着相当大的超限。我只是在一个示例中对其进行了测试,因为循环中的最后一段很长,这意味着整个代码返回了 1913 个字符。
猜你喜欢
  • 2013-06-12
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 2012-10-02
  • 2011-02-11
相关资源
最近更新 更多