简体   繁体   English

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

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

I am using using code with regular expression 我正在使用正则表达式的代码

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

and

$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

eg for searching "ह" look like this 例如,搜索“ह”看起来像这样

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

i want result 我想要结果

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

In english its proper working. 在英语中它正常工作。 eg for searching h 例如,搜索h

the Indian leadership 印度 领导人

please help me 请帮我

\\w or \\p{L} won't match मात्रा part of हैं . \\w\\p{L}हां的मात्रा部分हैं It will only match leaving मात्रा unmatched. 它只会匹配离开मात्रा无法比拟的。

You can use [^\\p{Zs}\\p{P}] instead to match any surrounding non-space, non-punctuation character. 您可以使用[^\\p{Zs}\\p{P}]来匹配任何周围的非空格,非标点字符。

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

Modifier /u is for unicode support. 修饰符/u用于unicode支持。

RegEx Demo RegEx演示

Code: 码:

$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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM