0

目标是计算一组用户文本中的段落数......

(对于这个练习,我假设它总是大于 5 段)

然后我想要 1/2 的段落数,四舍五入并echo "yehhoo"在中间输入一些内容()。

我确实理解我得到我的$newvalue方式不是很好,也希望得到帮助......

<?php

$choppedup = explode("<p>",$node->field_long_body[0]['safe']);
$choppedpos = count($choppedup);
$choppedpos2 = $choppedpos/2;
$newvalue = floor($choppedpos2);

//I know this is working to here... the rest not so sure.

for($j = 0; $j < $choppedup; $j ++):
  print $choppedup[$j];
  if ($j == $newvalue):
    echo "yehhoo" ;       
  endif;
endif;
?>

谢谢

4

3 回答 3

3
for 
...
endfor;       # not endif

你的$newvalue计算并不可怕,对于数组迭代我宁愿建议foreach循环(使用花括号):

foreach($choppedup as $ind => $p) {
    echo $p;
    if ($ind == $newvalue) {
        echo 'yehoo';
    }
}
于 2009-08-14T11:38:39.597 回答
1

大括号的“Yehhoo”!

for($j == 0; $j < $choppedup; $j ++) {
     print $choppedup[$j];
     if ($j == $newvalue) {
          echo "yehhoo";
     }
}
于 2009-08-14T11:51:18.383 回答
0

为什么要进行如此复杂的循环来计算段落标签的数量?

你可以这样做:

 $sInput = '<p>Hello World</p><p>What is going on</p><p>Blah</p>';
 if(preg_match_all('/<p>/', $sInput, $arMatches)!==false)
  print count($arMatches[0]) . ' paragraphs<br/>';

当然,上面需要一些工作来确保段落标签之间有文本,但这应该适合您的需要。

于 2009-08-14T17:41:24.517 回答