在 php 中使用 substr_count 时忽略大小写
Ingore case-sensitive when using substr_count in php
我正在制作一个字数统计程序,所以如果用户在文本框中键入一个字("hello")并提交表单(html),程序将搜索并统计它出现在特定网页上的次数。
我有这段代码,是西班牙语的,所以我会为你翻译它。
<?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";
}
}
}
?>
我的问题是,在网页上搜索时可以忽略大小写,
例如:hello-hEllo-Hello 都是一样的
如果您想要不区分大小写的 substr_count
函数,为什么不直接使用 strtolower
将输入转换为大小写不变的形式。
$count = substr_count(strtolower($haystack), strtolower($needle));
本机无法执行此操作。我会简单地这样做:
substr_count(strtolower($haystack), strtolower($needle));
我正在制作一个字数统计程序,所以如果用户在文本框中键入一个字("hello")并提交表单(html),程序将搜索并统计它出现在特定网页上的次数。
我有这段代码,是西班牙语的,所以我会为你翻译它。
<?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";
}
}
}
?>
我的问题是,在网页上搜索时可以忽略大小写, 例如:hello-hEllo-Hello 都是一样的
如果您想要不区分大小写的 substr_count
函数,为什么不直接使用 strtolower
将输入转换为大小写不变的形式。
$count = substr_count(strtolower($haystack), strtolower($needle));
本机无法执行此操作。我会简单地这样做:
substr_count(strtolower($haystack), strtolower($needle));