1

你好吗 ?

我有一段文字:

在 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)。

我为我的语言感到抱歉

4

2 回答 2

2
$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");
于 2012-06-03T15:38:19.197 回答
1

在不运行循环的情况下,您可以这样做:

$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
)
于 2012-06-03T15:48:22.153 回答