簡體   English   中英

如何用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">\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}हां的मात्रा部分हैं 它只會匹配離開मात्रा無法比擬的。

您可以使用[^\\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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM