Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

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

Hi everyone,

I've been working on a project where I have a text in Hindi and I want to highlight all the words in it using regular expressions in PHP. I understand that regular expressions can be quite powerful for text manipulation, but I'm not very experienced with them yet.

I've tried doing some research online, but I couldn't find a specific solution for my requirement. I would really appreciate it if someone could help me out with this. Is it possible to accomplish this task using regular expressions in PHP? If so, could you please provide me with a sample code or guide me in the right direction?

Thank you in advance for any help you can provide!

All Replies


Hi there,

I recently had a similar requirement where I needed to highlight specific words in Hindi text using regular expressions in PHP. I found that it is indeed possible to achieve this using regular expressions.

To highlight all the words, you can make use of the `preg_replace()` function in PHP. Here's an example code snippet that should help you get started:

$text = "मेरा नाम हिन्दी है। यह काफी रोचक और मनोहारी भाषा है।";
$highlightedText = preg_replace('/\b(\p{L}+)\b/u', '<span style="background-color: yellow;">$0</span>', $text);

echo $highlightedText;

In the above code, we're using the `\b(\p{L}+)\b` regular expression pattern. Let me break it down for you:

- `\b` matches a word boundary.
- `\p{L}` matches any Unicode letter.
- `+` specifies that we want to match one or more letters.
- `/u` modifier is used to treat the text as UTF-8.

The `preg_replace()` function replaces all matched words with the specified format, in this case, a span element with a yellow background color.

Give it a try and see if it works for you. Feel free to modify the code to fit your specific requirements. Don't hesitate to reach out if you have any further questions!

Best regards,
User 1


Namaste everyone,

I encountered a similar scenario in my PHP project where I needed to highlight specific Hindi words using regular expressions. After some experimentation, I discovered a solution that worked well for me.

To highlight all the words in Hindi text, you can utilize the `preg_replace_callback()` function along with a regular expression. Here's an example code snippet that you can try:

$text = "हमारे देश में हिन्दी अनेक स्थानीय भाषाओं में बोली जाती है। यह बहुत ही सुंदर और भावपूर्ण भाषा है।";
$highlightedText = preg_replace_callback('/(\p{L}+)/u', function($match) {
return '<span style="color: red;">' . $match[0] . '</span>';
}, $text);

echo $highlightedText;

In this example, we're using the `(\p{L}+)` regular expression pattern. Let me explain its components:

- `(\p{L}+)` matches one or more Unicode letters, denoted by `\p{L}`.
- `/u` is used to ensure proper handling of Unicode characters.

By utilizing `preg_replace_callback()`, we can apply a custom callback function to each match. In the provided code, we're wrapping the matched words with a `span` element and applying a red text color.

Feel free to modify the callback function and the styling according to your preferences. I hope this solution helps you achieve your goal. If you have any further queries, don't hesitate to ask.

Best regards,
User 3


Hey there,

I've faced a similar situation while working on a project involving Hindi text in PHP. Highlighting all the words can indeed be accomplished using regular expressions.

In my experience, I found that using the `preg_replace_callback()` function can provide more flexibility, especially when you need to highlight words based on certain conditions or add dynamic styling. Here's an example to help you out:

$text = "मैं छत्तीसगढ़ राज्य में रहता हूँ। मुझे हिंदी बोलना बहुत पसंद है।";
$highlightedText = preg_replace_callback('/\b(\p{L}+)\b/u', function($matches) {
return '<span style="font-weight: bold;">' . $matches[0] . '</span>';
}, $text);

echo $highlightedText;

In this approach, we're still using a similar regular expression pattern `\b(\p{L}+)\b` to match words. However, we're using `preg_replace_callback()` and passing a callback function to handle the replacement.

Inside the callback function, you have access to the matched word through the `$matches` variable. You can modify the callback function to apply different styles or conditions based on the word itself.

In this example, we're simply wrapping the matched word with a `span` element and applying a bold font style.

Feel free to customize the callback function and the styling as per your requirements. If you need any further assistance or have more questions, I'll be glad to help!

Best regards,
User 2

New to LearnPHP.org Community?

Join the community