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 关注
- 234 浏览
添加回答
举报