如何使用php在外部頁面中找到一個特定的詞? (DOM或pregmatch,或者還有什麼?)在foo.com源代碼在外部頁面中找到一個特定的詞
例如:
跨度NAME = 「ABCD」
我要檢查,如果字ABCD在foo.com在PHP
如何使用php在外部頁面中找到一個特定的詞? (DOM或pregmatch,或者還有什麼?)在foo.com源代碼在外部頁面中找到一個特定的詞
例如:
跨度NAME = 「ABCD」
我要檢查,如果字ABCD在foo.com在PHP
$v = file_get_contents("http://foo.com");
echo substr_count($v, 'abcd'); // number of occurences
//or single match
echo substr_count($v, ' abcd ');
if(preg_match('/span\s+name\=\"abcd\"/i', $str)) echo 'exists!';
要檢查是否存在字符的字符串:
<?php
$term = 'abcd';
if (preg_match("/$term/", $str)) {
// yes it does
}
?>
要檢查是否該字符串形式存在本身就是一個字(即,是不是在一個較大的單詞中間)使用單詞邊界匹配器:
<?php
$term = 'abcd';
if (preg_match("/\b$term\b/", $str)) {
// yes it does
}
?>
對於不區分大小寫搜索,添加i
標誌的最後一個斜線後的正則表達式:
<?php
$term = 'abcd';
if (preg_match("/\b$term\b/i", $str)) {
// yes it does
}
?>
以下是找到特定的詞等幾個方面
<?php
$str = 'span name="abcd"';
if (strstr($str, "abcd")) echo "Found: strstr\n";
if (strpos($str, "abcd")) echo "Found: strpos\n";
if (ereg("abcd", $str)) echo "Found: ereg\n";
if (substr_count($str, 'abcd')) echo "Found: substr_count\n";
?>
謝謝,非常好:) – julien