Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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_Regex_Preg Replace_Preg Match All - Fatal编程技术网

Php 正则表达式从字符串的第一个大写字母到句尾进行匹配,以突出显示单词数组

Php 正则表达式从字符串的第一个大写字母到句尾进行匹配,以突出显示单词数组,php,regex,preg-replace,preg-match-all,Php,Regex,Preg Replace,Preg Match All,我知道标题很难理解。 基本上我收到了一条短信,比如说大约20000个字符 当我执行搜索时,我想提取找到任何匹配单词的句子并突出显示它们 我得到了一个突出显示的单词数组$words,让我们调用主文本$text。 因此,我的代码如下: foreach($words as $word): $regex = '/[^.!?\n]*\b'.preg_quote($word,"/").'\b[^.!?\n]*/i'; preg_match_all($regex, $text, $matc

我知道标题很难理解。 基本上我收到了一条短信,比如说大约20000个字符

当我执行搜索时,我想提取找到任何匹配单词的句子并突出显示它们

我得到了一个突出显示的单词数组$words,让我们调用主文本$text。 因此,我的代码如下:

foreach($words as $word):

    $regex = '/[^.!?\n]*\b'.preg_quote($word,"/").'\b[^.!?\n]*/i';

    preg_match_all($regex, $text, $matches);  
    count($matches[0]) > 3 ? $search_q= 3 : $search_q=count($matches[0]);

    for ($i=0; $i < $search_q; $i++):
        echo preg_replace('/\b('.preg_quote($word,"/").')\b/i','<span class="highlighted">$1</span>',$matches[0][$i]).'[..]  ';
    endfor;
endforeach;
这些词是:

$words=array('social','media');
通过我的代码,我得到了以下信息:

A new holiday shopping tradition: Smartphones and **social** networks[..]
Many consumers will take out their phones before their wallets this holiday season with even more visiting **social** media sites before tackling their gift lists[..]
Additionally, 44 percent say they plan to use **social** media to seek discounts, read reviews and check family and friends’ gift lists[..]
Many consumers will take out their phones before their wallets this holiday season with even more visiting social **media** sites before tackling their gift lists[..]
Additionally, 44 percent say they plan to use social **media** to seek discounts, read reviews and check family and friends’ gift lists[..]
相反,我想要:

A new holiday shopping tradition: Smartphones and **social** networks[..]
Many consumers will take out their phones before their wallets this holiday season with even more visiting **social** **media** sites before tackling their gift lists[..]
Additionally, 44 percent say they plan to use **social** **media** to seek discounts, read reviews and check family and friends’ gift lists[..]
使用fge代码,我得到:

social[..] 
social[..] 
social[..] 
media[..] 
media[..] 

我希望举例说明这很容易理解。非常感谢

首先,我不明白为什么要使用如此复杂的正则表达式:您确实使用了单词锚,那么为什么还要麻烦使用补充字符类呢

其次,此解决方案假定单词不包含特殊的正则表达式字符

以下是您可以做的:

$w = preg_quote($word, "/");
$fullword = '\b\Q' . $w . '\E\b';
$regex = '/' . $fullword . '(?!.*' . $fullword . ')/i';
说明:
\Q
表示在
\E
之前,所有字符都应按字面意思处理(这意味着如果单词包含点,则您是安全的)。因此,你匹配你的单词(它被锚定),然后你说你不应该再匹配单词
(?!.*\b\Qwordhere\E\b)

这意味着,如果一个句子多次包含该单词,它将只匹配最后一次出现的单词

最后,要突出显示,请使用:

preg_replace('/(' . $fullword . ')/ig', '<span class="highlighted">$1</span>', $text);
preg_replace('/('.$fullword')/ig'、'$1'、$text);

首先,我不明白为什么要使用如此复杂的正则表达式:您确实使用了单词锚,那么为什么还要麻烦使用补充字符类呢

其次,此解决方案假定单词不包含特殊的正则表达式字符

以下是您可以做的:

$w = preg_quote($word, "/");
$fullword = '\b\Q' . $w . '\E\b';
$regex = '/' . $fullword . '(?!.*' . $fullword . ')/i';
说明:
\Q
表示在
\E
之前,所有字符都应按字面意思处理(这意味着如果单词包含点,则您是安全的)。因此,你匹配你的单词(它被锚定),然后你说你不应该再匹配单词
(?!.*\b\Qwordhere\E\b)

这意味着,如果一个句子多次包含该单词,它将只匹配最后一次出现的单词

最后,要突出显示,请使用:

preg_replace('/(' . $fullword . ')/ig', '<span class="highlighted">$1</span>', $text);
preg_replace('/('.$fullword')/ig'、'$1'、$text);

如果你将文本分成一系列句子,然后依次检查每个句子,你的头部可能会受到较少的伤害。如果单词列表不太长,可以将整个列表放入正则表达式中。比如:

/\b(\Qword1\E|\Qword2\E|\Qword3\E)\b/

如果你把课文分成一系列句子,然后依次检查每一个句子,你的大脑可能会受到更少的伤害。如果单词列表不太长,可以将整个列表放入正则表达式中。比如:

/\b(\Qword1\E|\Qword2\E|\Qword3\E)\b/

我尝试了你的代码,但我只是得到了单词,而不是完整的句子。我会用一个例子来更新这个问题。我试过你的代码,但我只是得到了单词,而不是完整的句子。我将用一个例子更新这个问题对不起,我不知道如何在我的代码上实现这个,im regex null guy:D。我用例子更新了我的问题。对不起,我不知道如何在我的代码im regex null上实现这个。我用例子更新了我的问题。