在文本中搜索,然后按PHP选择关键字


search in text , and select keywords by php

你好吗?

我有一条短信:

拜占庭人在7世纪初短暂的波斯入侵后重新控制了该国,直到639-42年,埃及被穆斯林阿拉伯人入侵并被伊斯兰帝国征服。当他们在埃及击败拜占庭军队时,阿拉伯人将逊尼派伊斯兰教带到了该国。在这一时期的早期,埃及人开始将他们的新信仰与土著信仰和实践融合在一起,导致了各种苏菲教派的繁荣,直到今天。[24]这些早期的仪式在科普特基督教时期幸存下来

我想在此文本中搜索,并选择类别,标签

<?php // this is some tags in Array , but I don't know which tag is used in this text.
$search_words = array("Egypt , Persian , Islamic , USA , Japan , Spain , Saudi Arabia");
foreach($search_words as $value){
        stristr($longText, $search_words); // I know this is mistake
    }?>

我想选择在这个简单中使用的单词($search_words)。

对不起我的语言

$words_found = array();
foreach ($search_words as $word) {
    if (stristr($longText, $word)) {
         $words_found[] = $word;
    }
}

$words_found现在是一个数组,其中包含文本中存在的$search_words数组中的所有标签。

另外,您的示例中数组的语法不正确,应该是这样的:

$search_words = array("Egypt", "Persian", "Islamic", "USA", "Japan", "Spain", "Saudi Arabia");

无需运行循环,您可以执行以下操作:

$str = <<< EOF
The Byzantines were able to regain control of the country after a brief Persian
invasion early in the 7th century, until 639-42, when Egypt was invaded and conquered
by the Islamic empire by the Muslim Arabs. When they defeated the Byzantine Armies in
Egypt, the Arabs brought Sunni Islam to the country. Early in this period, Egyptians
began to blend their new faith with indigenous beliefs and practices, leading to 
various Sufi orders that have flourished to this day.[24] These earlier rites had
survived the period of Coptic Christianity in Saudi Arab.
EOF;
$search_words = array("Egypt", "Persian", "Islamic", "USA", "Japan", "Spain",
                      "Saudi Arab");
preg_match_all('/'b[A-Z][a-z'd]*(?:'s+[A-Z][a-z'd]*)?'b/', $str, $arr);
print_r(array_intersect($search_words, $arr[0]));

输出:

Array
(
    [0] => Egypt
    [1] => Persian
    [2] => Islamic
    [6] => Saudi Arab
)