Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用哈希表将关键字搜索到文本中_Php_Hashtable_Search - Fatal编程技术网

Php 使用哈希表将关键字搜索到文本中

Php 使用哈希表将关键字搜索到文本中,php,hashtable,search,Php,Hashtable,Search,我想在文本中搜索关键字,我有大约6000个关键字,我需要知道在PHP中最好的方法是什么 在php中实现哈希表的最佳方法是什么?一个带有关联键的简单数组怎么样?PHP的数组已经用哈希表实现了。正则表达式更好,因为这样可以测试单词边界。另外,它可能更快 话虽如此,还是有点道理 $needles = array('cat','dog','fox','cow'); $haystack = 'I like cats and dogs, but my favorie animal is the cow';

我想在文本中搜索关键字,我有大约6000个关键字,我需要知道在PHP中最好的方法是什么


在php中实现哈希表的最佳方法是什么?

一个带有关联键的简单数组怎么样?PHP的数组已经用哈希表实现了。

正则表达式更好,因为这样可以测试单词边界。另外,它可能更快

话虽如此,还是有点道理

$needles = array('cat','dog','fox','cow');
$haystack = 'I like cats and dogs, but my favorie animal is the cow';

$hash = array();
foreach($needles as $needle){
    if (strstr($haystack,$needle)){
        $hash[$needle] = 1;
    }
}

echo "<pre>";
print_r(array_keys($hash));
echo "</pre>";
$niners=数组('cat'、'dog'、'fox'、'cow');
$haystack='我喜欢猫和狗,但我最喜欢的动物是牛';
$hash=array();
foreach($针作为$针){
if(strstr($haystack,$Pine针)){
$hash[$needle]=1;
}
}
回声“;
打印(数组密钥($hash));
回声“;

这个问题有点模糊,所以我选择了一个简单的场景/解决方案;-)

印刷品

这意味着:关键字
PHP
被发现157次,
call
7次,
supercalifragilistic
0次


请随时详细说明您正在尝试什么和需要什么……

嗯,好吧,但是,如何在一个regexp中测试6000个单词?(它们有不同的模式)正则表达式将取代strstr函数。strstr函数只是查看目标字符串是否在字符串中。嗯,好的,我发现:
$keywords = array('PHP', 'introduction', 'call', 'supercalifragilistic');
$text = file_get_contents('http://www.php.net/archive/2009.php'); // including all the html markup, only an example
$words = array_count_values(str_word_count($text, 2));
$result = array_intersect_key($words, array_flip($keywords));
var_dump($result);
array(2) {
  ["PHP"]=>
  int(157)
  ["call"]=>
  int(7)
}