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

在文本中搜索,并通过php选择关键字,php,tags,Php,Tags,你好吗 我有一段文字: 拜占庭人在7世纪初波斯短暂入侵埃及后重新获得了对该国的控制权,直到639-632年,埃及被伊斯兰帝国的穆斯林阿拉伯人入侵并征服。当阿拉伯人在埃及击败拜占庭军队时,他们将逊尼派伊斯兰教带到了埃及。早在这一时期,埃及人就开始将他们的新信仰与本土信仰和习俗相融合,从而形成了各种苏菲教派,并发展到今天。[24]这些早期的仪式在科普特基督教时期得以延续 我想在此文本中搜索,并选择类别、标签 <?php // this is some tags in Array , but I

你好吗

我有一段文字:

拜占庭人在7世纪初波斯短暂入侵埃及后重新获得了对该国的控制权,直到639-632年,埃及被伊斯兰帝国的穆斯林阿拉伯人入侵并征服。当阿拉伯人在埃及击败拜占庭军队时,他们将逊尼派伊斯兰教带到了埃及。早在这一时期,埃及人就开始将他们的新信仰与本土信仰和习俗相融合,从而形成了各种苏菲教派,并发展到今天。[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
    }?>

$words\u found
现在是一个数组,包含文本中存在的
$search\u 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\u intersect
中的参数或查看我的编辑。另外,现在我捕获了多达2个单词,因为您在搜索关键字中有
“沙特阿拉伯”
。RegEx并不是万能的答案。上面的答案更简单,可读性更强。@Truth:这个答案很简单,但不准确,因为它也将匹配
'Japanican','Spaint'
,而不是
'Japan','Spaint'
,仅举一些例子。
$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
)