[英]Why does this count always return 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;
}
使用strlen( $str )
來計算字母:
$myStr = 'Hello world';
echo strlen($myStr); // outputs 11
Strlen意味着Str ing Len gth。
您可能希望使用strlen()
而不是count()
。 我認為count()
轉換為數組,然后計算該數組中的元素數,在本例中為1
。
我想你想要strlen()
函數,而不是count()
。
如果你使用utf-8編碼(非拉丁字符)mb_strlen()會更准確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.