PHP substr_count () function
contoh
Menghitung jumlah "dunia" muncul dalam string:
<?php
echo substr_count("Hello world. The world is nice","world");
?>
echo substr_count("Hello world. The world is nice","world");
?>
Menjalankan contoh »
substr_count () fungsi menghitung jumlah kejadian dalam substring tali.
Catatan: substring adalah case-sensitive.
Catatan: Fungsi ini tidak dihitung substring tumpang tindih (lihat Contoh 2).
CATATAN: Jika Anda memulai argumen ditambah panjang parameter lebih besar dari panjang string, fungsi akan menghasilkan peringatan (lihat Contoh 3).
tatabahasa
substr_count( string,substring,start,length )
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
substring | 必需。规定要检索的字符串。 |
start | 可选。规定在字符串中何处开始搜索。 |
length | 可选。规定搜索的长度。 |
rincian teknis
Pengembalian: | Mengembalikan jumlah kejadian dalam substring tali. |
---|---|
Versi PHP: | 4+ |
Update log: | Dalam PHP 5.1, menambahkan awal dan panjang parameter. |
contoh yang lebih
contoh 1
Menggunakan semua parameter:
<?php
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is PHP"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is PHP"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is PHP"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>
Menjalankan contoh »
contoh 2
substring tumpang tindih:
<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>
Menjalankan contoh »
contoh 3
Jika awal dan panjang parameter lebih panjang dari string, fungsi output peringatan:
<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>
Karena panjang nilai melebihi panjang dari string (3 + 9 besar dari 12). Jadi itu akan menampilkan peringatan.
PHP String Reference