如何計算數字與PHPPHP:如何計數?
$mynumbers="0855 468 4864 63848 1486"; //This is my variable, the pattern has to be like this, each number is separated by a space.
echo "There are 5 numbers in your variable";
它應該返回: 5
我怎麼做,我知道有str_word_count
但只計算的話不是數字。
拆分空格上的字符串並計算您獲得的項目數。如果你需要忽略非數字值(例如'ignorethis 123和this'),那麼你可以遍歷結果中的每個項目,並且如果它們被認爲是數字,則增加一個計數器。 – GoBusto 2015-02-23 10:35:15
使用,例如, '$ exploded = explode('',$ mynumbers); echo'There''.count($ exploded)'。'數字在你的變量中;';' – lmarcelocc 2015-02-23 10:36:55
幾乎所有的解決方案都是矯枉過正的......如果你確定這個字符串只包含數字和空格,你應該計算空格並添加一個。如果你想在將來有數組,使用爆炸:)。 – 2015-02-23 10:54:28