Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php,而在for循环中,使用strlen_Php_Loops_For Loop_While Loop_Strlen - Fatal编程技术网

php,而在for循环中,使用strlen

php,而在for循环中,使用strlen,php,loops,for-loop,while-loop,strlen,Php,Loops,For Loop,While Loop,Strlen,下面是一个代码块,在获取所需数据方面运行良好。 别笑,这可能效率低下,但我正在学习:) 我想要的是,使用$totalLength变量,当$totalLength为1500字节/字符时停止收集数据(理想情况下,以一个完整的单词结尾,但我不是在寻找奇迹!)。无论如何,代码: $paraLength = 0; $totalLength = 0; for ($k = 0; $k < $descriptionValue->length; $k++) { //define inte

下面是一个代码块,在获取所需数据方面运行良好。 别笑,这可能效率低下,但我正在学习:) 我想要的是,使用$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
}
}
$paraLength=0;
$totalength=0;
对于($k=0;$k<$descriptionValue->length;$k++){//将整数k定义为0,使用($k=0;$k<$descriptionValue->length;$k++)获取每个描述,增加k循环(若要仅获取14个元素,请使用($k item($k)->nodeValue;//查找每个描述元素
$descNode=trim($descNode);//修剪元素周围的所有空格
$descPara=strip_tags($descNode);//从元素中删除任何HTML标记
$paraLength=(strlen($descPara));//查找每个元素的长度
//if(preg_match('/^([0-9]+)$/',$descPara)){//如果元素以数字开头,后跟空格,则将其定义为电话号码
//$number=$descPara;
//fwrite($fh,“\t\t.”.$number.“\n”);//写一个id为tel的div,其中包含数字
//}
//否则
if(preg_match('/[A-Z]{4,}/',$descPara)){//if元素至少以4个大写字符开头,将其定义为标题
$heading=$descPara;
$heading=ucfirst(strtolower($heading));//将大写字符串转换为正确的
fwrite($fh,“\t\t.”.$heading.\n”);//编写一个id为heading的div,在h4标记中包含该heading
}
else if(preg_match('/\d*\.\d{1,}[m x]/',$descPara)){//如果元素包含任意数量的数字,后跟一个点,至少还有一个数字和字母m x,则根据包含房间测量值的元素将其定义为标题(此模式与点\d*{2,}后面的至少两个数字匹配)
$room=$descPara;
fwrite($fh,“\t\t.”.$room.\n”);//编写一个id为roomheading的div,将标题包含在h4标记中
}
else if(preg_match('/^Disclaimer/i',$descPara)){//如果元素包含单词Disclaimer,则将其定义为
$disclaimer=$descPara;
fwrite($fh,“\t\t.”.$disclaimer.\n”);//编写一个带有id disclaimer的div,包含h4标记中的标题
}
else if(strlen($paraLength3)){//当所有其他操作都失败时,如果元素少于14个字符但多于3个字符,也将其定义为标题
$other=$descPara;
fwrite($fh,“\t\t.”.$other.\n”);//用id other和h4标记中的标题编写一个div
}
否则{
fwrite($fh,“\t\t\t”“$descPara。”

\n”);//任何其他内容都被视为内容,所以请将其写在p标记中 } }
$totalLength很好,但当我尝试在其中放入while语句时,它挂起了。我尝试在for前后放入while语句,但没有乐趣。我做错了什么,如何最好地解决这个问题


仅供参考$descriptionValue,是使用DOM和xpath从HTML解析的数据,而我尝试的是while($totalLength也许这就是您想要的:

if ($totalLength > 1500) {
    break;
}

也许这就是你想要的:

if ($totalLength > 1500) {
    break;
}

只需在你的
for
循环中放入一个条件。一旦条件计算为true,它就会跳出循环

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

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

只需在
for
循环中添加一个条件。一旦条件计算为true,它就会跳出循环

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

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

您还可以删除for并添加

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

在循环中,您可以增加值ok$k

,也可以删除for并添加

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

在循环中增加值ok$k

如果缩进代码就好了;-)你想把while语句放在哪里?在哪里?如果缩进代码就好了;-)你想把while语句放在哪里?在哪里?我喜欢这个解决方案,因为它意味着没有分词,但它可能意味着大量的过度运行。我只是在一个例子上测试了它,因为循环的最后一段相当长,它意味着整个代码返回1913个字符。我喜欢这个解决方案,因为它意味着没有分词,但是它可能意味着大量的过度运行。我刚刚在一个示例上测试了它,因为循环中的最后一段相当长,这意味着整个代码返回了1913个字符。我喜欢这个声音,因为我想用while替换for循环,但是,我无法让你的建议起作用。我将你的建议替换为for stat每一个循环中都有一个元素和$k++。你能给我一个你认为它如何工作的例子吗?我喜欢这个声音,因为我想用while替换for循环,但是,我无法让你的建议起作用。我把你的建议放在for语句和$k++的位置上。你能给我一个你如何看待它的例子吗工作,请?谢谢你bth,这在一定程度上是有效的。正如我在回复Alvar时提到的,这意味着结果可能会超过1500个字符。我真的需要了解现代编程语言:D。我在Gosub Return、Goto和Sinclair Basic中学习了下一个!一旦这个项目完成,我将努力学习。谢谢你bth,这在一定程度上是有效的。正如我在回复Alvar时提到的,这意味着结果可能会超过1500个字符。我真的需要了解现代编程语言:D。我在Gosub Return、Goto和