在文本中搜索,并通过php选择关键字

时间:2012-06-03 15:29:58

标签: php tags

你好吗?

我有一个文字:

  拜占庭人在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)。

对不起我的语言

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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
)