Nitin Karale Nitin Karale - 4 months ago 24
PHP Question

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);


$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

e.g. for searching "ह" look like this

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

i want result

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

In english its proper working. e.g for searching h

the Indian leadership

please help me


\w or \p{L} won't match मात्रा part of हैं. It will only match leaving मात्रा unmatched.

You can use [^\p{Zs}\p{P}] instead to match any surrounding non-space, non-punctuation character.


Modifier /u is for unicode support.

RegEx Demo


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