Php 为什么这个计数总是返回1?
我有下面的函数来计算标记中的字符数。输出始终为“1”,即使我知道它大于一位数。我做错了什么Php 为什么这个计数总是返回1?,php,regex,function,count,Php,Regex,Function,Count,我有下面的函数来计算标记中的字符数。输出始终为“1”,即使我知道它大于一位数。我做错了什么 $www = $_POST['url']; $url = file_get_contents($www); [更多代码] function countTitle() { global $url; $search = "/\<title\>(.*)\<\/title\>/"; preg_match($search, $url, $result); $title = $resul
$www = $_POST['url'];
$url = file_get_contents($www);
[更多代码]
function countTitle() {
global $url;
$search = "/\<title\>(.*)\<\/title\>/";
preg_match($search, $url, $result);
$title = $result[1]; // to string
$counttitle = count($title);
echo $counttitle;
}
函数countTitle(){
全球$url;
$search=“/\(.*)\/”;
预匹配($search,$url,$result);
$title=$result[1];//到字符串
$counttitle=计数($title);
echo$counttitle;
}
我知道regexp可以工作,因为我使用以下函数来回显title标记:
function getTitle() {
global $url;
$search = "/\<title\>(.*)\<\/title\>/";
preg_match($search, $url, $result);
$title = $result[1]; // to string
echo $title;
}
函数getTitle(){
全球$url;
$search=“/\(.*)\/”;
预匹配($search,$url,$result);
$title=$result[1];//到字符串
echo$标题;
}
用于计数字母:
$myStr = 'Hello world';
echo strlen($myStr); // outputs 11
Strlen的意思是StringLength。我想你想要的是
Strlen()
函数,而不是count()你可能想用Strlen()
而不是count()
。我认为count()
首先强制转换到一个数组,然后计算该数组中的元素数,在本例中为1
如果您使用utf-8编码(非拉丁字符),mb_strlen()将更准确。您是否正在清理$www
的值?如果$\u POST['url
]`是服务器上不希望可读的文件的url或路径,会发生什么情况?