如何在php中用正则表达式突出显示印地语文本中的所有单词


How to highlight all words in hindi text with regular expression in php?

我使用的是带有正则表达式的using代码

 $title =  preg_replace("/'b(".preg_quote($searchword).")'b/i", '<span class="highlight_txt">'1</span>', $title);

$title = preg_replace("/'w*?".preg_quote($searchword)."'w*/i", "<span class='highlight_txt'>$0</span>", $title);
It is working properly for english but not for hindi string

例如用于搜索"ह"看起来像这个

तरके लाभ मिलते ैं

我想要结果

तरहके लाभ मिलते हैं

在英语中,这是恰当的工作方式。例如搜索h

印度领导层

请帮我

'w'p{L}不匹配मात्रा部分CCD_ 3。它将只匹配离开मात्रा无与伦比。

您可以使用[^'p{Zs}'p{P}]来匹配任何周围的非空格、非标点符号。

/[^'p{Zs}'p{P}]*?ह[^'p{Zs}'p{P}]*/u

修饰符/u用于支持unicode。

RegEx演示

代码:

$title = 'तरह के लाभ मिलते हैं';
$searchword = 'ह';
$title = preg_replace('/[^'p{Zs}'p{P}]*?'.preg_quote($searchword).'[^'p{Zs}'p{P}]*/u',
         "<span class='highlight_txt'>$0</span>", $title);
//=> <span class='highlight_txt'>तरह</span> के लाभ मिलते <span class='highlight_txt'>हैं</span>