Php 计数($array)在if语句中不起作用

Php 计数($array)在if语句中不起作用,php,arrays,Php,Arrays,我正在尝试将一个fade类应用于所有100字或更多的字符串 以下是我目前掌握的代码: $descArray = explode(" ", stripslashes($longDesc)); echo "word count for array: ".count($descArray); $shortDesc = implode(" ", array_splice($descArray, 0, 100)); if(count($descArray) >= 100) { print "

我正在尝试将一个
fade
类应用于所有100字或更多的字符串

以下是我目前掌握的代码:

$descArray = explode(" ", stripslashes($longDesc));
echo "word count for array: ".count($descArray);
$shortDesc = implode(" ", array_splice($descArray, 0, 100));
if(count($descArray) >= 100) {
    print "<div class='fade'>".$shortDesc."</div>";
} else {
    print "<div>".$shortDesc."</div>";
}
$descArray=explode(“,stripslashes($longDesc));
echo“数组的字数:”.count($descArray);
$shortDesc=内爆(“,数组拼接($descArray,01100));
如果(计数($descArray)>=100){
打印“$shortDesc.”;
}否则{
打印“$shortDesc.”;
}
根据审查后的源代码,
fade
类仅应用于(看起来是什么)长度为200或更多的字符串,即使指定将该类应用于长度为100或更多的所有字符串。我用一个lorem生成器进行了测试——一个字符串被拆分成199个数组时,从未应用过fade类,但当拆分成200个数组时,它确实应用了fade类


我肯定我在这里犯了一个愚蠢的错误,但这是什么?

为什么不使用
str\u word\u count()
php函数来实现您试图实现的功能

试试这个:

$descArray = explode(" ", stripslashes($longDesc));
$shortDesc = stripslashes($longDesc);
$wordCount = str_word_count($shortDesc);

if($wordCount < 100) {
   echo "<div>".$shortDesc."</div>";
} else {
   $shortDesc = implode(" ", array_splice($descArray, 0, 100))
   echo '<div class="fade">'.$shortDesc.'</div>';
} 
$descArray=explode(“,stripslashes($longDesc));
$shortDesc=stripslashes($longDesc);
$wordCount=str\u word\u count($shortDesc);
如果($wordCount<100){
回显“$shortDesc.”;
}否则{
$shortDesc=内爆(“,阵列拼接($descArray,01100))
回显“.$shortDesc.”;
} 

为什么不使用
str\u word\u count()
php函数来实现您试图实现的功能

试试这个:

$descArray = explode(" ", stripslashes($longDesc));
$shortDesc = stripslashes($longDesc);
$wordCount = str_word_count($shortDesc);

if($wordCount < 100) {
   echo "<div>".$shortDesc."</div>";
} else {
   $shortDesc = implode(" ", array_splice($descArray, 0, 100))
   echo '<div class="fade">'.$shortDesc.'</div>';
} 
$descArray=explode(“,stripslashes($longDesc));
$shortDesc=stripslashes($longDesc);
$wordCount=str\u word\u count($shortDesc);
如果($wordCount<100){
回显“$shortDesc.”;
}否则{
$shortDesc=内爆(“,阵列拼接($descArray,01100))
回显“.$shortDesc.”;
} 

在获得计数之前,您正在拼接阵列。将其拼接并分配给自身

$array = array(1,2,3,4,5,6,7,8,9);
array_splice($array, 0,3);
print_r($array);
新数组将是
数组(1,2,3,4)


计算文本大小和单词数是一个更好的选择。

在获得计数之前,您正在拼接数组。将其拼接并分配给自身

$array = array(1,2,3,4,5,6,7,8,9);
array_splice($array, 0,3);
print_r($array);
新数组将是
数组(1,2,3,4)


计算文本大小和单词数是更好的选择。

计算字符串长度是一个strlen函数。

计算字符串长度是一个strlen函数。

如果(count($descArray)>=100){
如果(strlen($descArray)>=100){
@Rizier123我想他想要的是字数而不是字符串长度
if(count($descArray)>=100){
->
if(strlen($descArray)>=100){
@Rizier123我想他想要的是字数而不是字符串长度