Home > front end >  Replace Last Character From All words of String PHP
Replace Last Character From All words of String PHP

Time:06-26

I am new in PHP. I have string with multiple words like this

Tamari bhula
kadhanara vyaktine olakho,
eka divasa e ja tamane unchaio
para pahonchadase!!

What I am looking for is remove last character from word if it have a at the end of word.

Expected output is like this

Tamari bhul
kadhanar vyaktine olakho,
ek divas e j tamane unchaio
par pahonchadase!!

Thanks!

CodePudding user response:

You can match an a which is the last character in a word using this regex:

a\b

which looks for an a followed by a word break. You can then replace those with '' (empty string).

$text = 'Tamari bhula
kadhanara vyaktine olakho,
eka divasa e ja tamane unchaio
para pahonchadase!!';

$new = preg_replace('/a\b/', '', $text);
echo $new;

Output:

Tamari bhul
kadhanar vyaktine olakho,
ek divas e j tamane unchaio
par pahonchadase!!

Demo on 3v4l.org

  • Related