[英]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支持。
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.