2011-10-21 42 views
0

如何使用php在外部頁面中找到一個特定的詞? (DOM或pregmatch,或者還有什麼?)在foo.com源代碼在外部頁面中找到一個特定的詞

例如:

跨度NAME = 「ABCD」

我要檢查,如果字ABCD在foo.com在PHP

回答

1
$v = file_get_contents("http://foo.com"); 
echo substr_count($v, 'abcd'); // number of occurences 

//or single match 

echo substr_count($v, ' abcd '); 
+0

謝謝,非常好:) – julien

1
if(preg_match('/span\s+name\=\"abcd\"/i', $str)) echo 'exists!'; 
1

要檢查是否存在字符的字符串:

<?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 

} 

?> 
0

以下是找到特定的詞等幾個方面

<?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"; 
?> 
相關問題