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ループ内に条件を入れるだけです。条件が true と評価されるとすぐに、ループの外にジャンプします。

// 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 に答える