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

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

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

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

$title = preg_replace("/\w*?".preg_quote($searchword)."\w*/i", "<span class='highlight_txt'>[=12=]</span>", $title);

It is working properly for english but not for hindi string

例如搜索“ह”看起来像这样

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

我想要结果

तरह के लाभ मिलते हैं[=3​​7=]

在英语中它的正常工作。例如搜索 h

印度领导层

请帮帮我

\w\p{L} 不会匹配 मात्राहैं 部分。它只会匹配 मात्रा 不匹配。

您可以使用 [^\p{Zs}\p{P}] 来匹配任何周围的非 space、非标点字符。

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

修饰符 /u 用于 unicode 支持。

RegEx Demo

代码:

$title = 'तरह के लाभ मिलते हैं';
$searchword = 'ह';

$title = preg_replace('/[^\p{Zs}\p{P}]*?'.preg_quote($searchword).'[^\p{Zs}\p{P}]*/u',
         "<span class='highlight_txt'>[=11=]</span>", $title);

//=> <span class='highlight_txt'>तरह</span> के लाभ मिलते <span class='highlight_txt'>हैं</span>