2 回答

TA貢獻1820條經驗 獲得超10個贊
您可以使用正則表達式輕松計算點數:
PHP
preg_match_all("/\./", "45FGG.TESTDOC.MAY12.png"); // 3
JS
"45FGG.TESTDOC.MAY12.png".match(/\./g).length; // 3

TA貢獻1963條經驗 獲得超6個贊
下面是兩個測試用例:
$file1 = "hello.jpg";
$file2 = "hello.file.jpg";
echo "\$file1: " . $file1 . "\n";
echo "\$file2: " . $file2 . "\n\n";
echo "Position of \$file1's first '.' : " . strpos($file1, '.') . "\n";
echo "Position of \$file1's last '.' : " . strripos($file1, '.') . "\n";
echo "Position of \$file2's first '.' : " . strpos($file2, '.') . "\n";
echo "Position of \$file2's last '.' : " . strripos($file2, '.') . "\n\n";
if (strpos($file1, '.') != strripos($file1, '.'))
{
echo "\$file1 has >= 2 dots\n";
}
else
{
echo "\$file1 has 1 dot \n";
}
if (strpos($file2, '.') != strripos($file2, '.'))
{
echo "\$file2 has >= 2 dots\n";
}
else
{
echo "\$file2 has 1 dot \n";
}
輸出:
$file1: hello.jpg
$file2: hello.file.jpg
Position of $file1's first '.' : 5
Position of $file1's last '.' : 5
Position of $file2's first '.' : 5
Position of $file2's last '.' : 10
$file1 has 1 dot
$file2 has >= 2 dots
- 2 回答
- 0 關注
- 230 瀏覽
添加回答
舉報