为什么这个计数总是返回 1?
我有以下函数来计算标签中的字符数。输出始终为“1”,即使我知道它不仅仅是一个数字。我做错了什么?
$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;
}
我知道正则表达式有效,因为我使用以下函数来回显标题标签:
function getTitle() {
global $url;
$search = "/\<title\>(.*)\<\/title\>/";
preg_match($search, $url, $result);
$title = $result[1]; // to string
echo $title;
}
I have the following function to count the amount of characters in the tag. The output is always '1' even when I know for a fact it is more then a single digit. What am I doing wrong?
$www = $_POST['url'];
$url = file_get_contents($www);
[some more code]
function countTitle() {
global $url;
$search = "/\<title\>(.*)\<\/title\>/";
preg_match($search, $url, $result);
$title = $result[1]; // to string
$counttitle = count($title);
echo $counttitle;
}
I know the regexp works because I use the following function to echo the title tag:
function getTitle() {
global $url;
$search = "/\<title\>(.*)\<\/title\>/";
preg_match($search, $url, $result);
$title = $result[1]; // to string
echo $title;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
strlen( $str )
来计算字母数:Strlen 表示 Str< /strong> ing 长度 gth。
Use
strlen( $str )
to count the letters:Strlen means Str ing Len gth.
您可能想使用
strlen()
而不是count()
。我认为count()
首先转换为数组,然后计算该数组中的元素数量,在本例中为1
。http://php.net/manual/en/function.strlen.php
http://php.net/manual/en/function.count.php
You probably want to use
strlen()
instead ofcount()
. I thinkcount()
casts to an array first, and then counts the number of elements in that array, which in this case is1
.http://php.net/manual/en/function.strlen.php
http://php.net/manual/en/function.count.php
我认为您需要
strlen()
函数,而不是count()
。I think you want the
strlen()
function, notcount()
.如果您使用 utf-8 编码(非拉丁字符),mb_strlen() 会更准确。
And if you are using utf-8 encoding (non Latin characters) mb_strlen() will be more accurate.