站长论坛

标题: 字符串处理函数之一substr_count(string1,string2) [打印本页]

作者: tznktg    时间: 2007-10-4 15:13
标题: 字符串处理函数之一substr_count(string1,string2)
function substr_count(string1,string2)
功能:统计string2在string1中出现的次数.

例如:
<?
$countnum=substr_count('sdaf#jsalf#ksdajfl#jlkjl##','#');
echo "#出现的次数:".$countnum;
?>

网页显示:
出现的次数:5
作者: tznktg    时间: 2007-10-4 15:14
范例 substr_count()

范例
例子 1. A substr_count() example

<?php
$text = 'This is a test';
echo strlen($text); // 14

echo substr_count($text, 'is'); // 2

// the string is reduced to 's is a test', so it prints 1
echo substr_count($text, 'is', 3);

// the text is reduced to 's i', so it prints 0
echo substr_count($text, 'is', 3, 3);

// generates a warning because 5+10 > 14
echo substr_count($text, 'is', 5, 10);


// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
?>




欢迎光临 站长论坛 (http://www.tzlink.com/bbs/) Powered by Discuz! X3.2