2017-02-23 39 views
0

我想提出一個字計數程序substr_count,因此如果用戶鍵入的文本就像一個字(「你好」),並提交表單(HTML),該程序將搜索和計數它在特定網頁上顯示的次數。
我有這樣的代碼,是西班牙語,所以我將它平移,爲你。則會忽略大小寫敏感的,當使用PHP

<?php 

/*if the user submit, do this...*/ 

if(isset($_POST["submit"])) { 

/*no warnings*/ 

error_reporting(E_ALL^E_WARNING); 

$buscar = $_POST["texto"]; 

$pagina = $_POST["Web"]; 

$buscar2 = strtolower($buscar); 

/*no special characters like !hello!*/ 

$buscar2 = preg_replace("/[^A-Za-z0-9]/", "", $buscar2); 

/*if the webpage doesn't exist, display this message*/ 

$headers = @get_headers($pagina); 

if(strpos($headers[0],'200')===false) 

{echo "Página no válida.";}else{ 

/*Get all content from the webpage*/ 

$web = file_get_contents($pagina); 

/*Word - count the number of times it appears on a webpage*/ 

$result = (substr_count(strip_tags($web), $buscar2)); 

/*Display results*/ 

if($result == 0){ 

echo "La palabra " .strtoupper($buscar2). " no aparece"; 

}else{ 

echo "La palabra " .strtoupper($buscar2). " aparece: $result veces"; 
} 
} 
} 
?> 

我的問題是,有可能當你在網頁上搜索忽略大小寫, 例如:您好,您好,您好是相同的。

+1

旁註:'如果(isset($ _ POST [ 「提交」])){ /*沒有任何警告*/ 的error_reporting(E_ALL^E_WARNING);',將只如果輸入被點擊,則會出現。如果該提交沒有命名,您的代碼將無提示失敗。在第一行放置錯誤檢查;始終/不管。 –

+0

和'@'是錯誤抑制器BTW和HTML表單是未知的。 –

+1

你應該小寫字和文本。這就是你可以如何搜索不區分大小寫的。 – Alex

回答

1

原生沒有辦法做到這一點。我只想做到這一點:

substr_count(strtolower($haystack), strtolower($needle)); 
2

如果您想區分大小寫substr_count函數,爲什麼不只是使用strtolower來轉換不變形式的輸入。

$count = substr_count(strtolower($haystack), strtolower($needle));