Remove emoji - PHP function

Hello!

I’m trying to remove emojis from my title tag and I want to find a nice way to do it.

Unfortunately there is no equivalent method available like Html::decode().

I use a function in PHP with preg_replace (see https://stackoverflow.com/questions/12807176/php-writing-a-simple-removeemoji-function) but I don’t know where to put this function outside of my template to have a clean code.

Should I simply use a snippet?

Welcom to the community :slight_smile:

Probably makes sense to do it as a field method, then you can probably use it on any field, not just the title.

2 Likes

For your use case a field function makes absolute sense. Just as additional information: You can put any function you will over need into a plugin file.

2 Likes

Look at e.g. https://getkirby.com/docs/guide/plugins/plugin-basics and the other docs…

Thank you!

I’m still very new to web development and I am discovering what Kirby can do.
I’ll give a look at all the information given and will update the thread.

Regards,

Luce

Took me some time to finally try but thanks to your answers I’ve been able to achieve what I wanted.

I’ve actually followed @texnixe advise and just put the PHP function I used in the index.php file of the plugin.

Might not be perfect yet but at least it is working properly and my templates are much cleaner!

I’m using this function for those that would be interested:

<?php

function removeEmoji($text) {

    $text = iconv('UTF-8', 'ISO-8859-15//IGNORE', $text);
    $text = preg_replace('/\s+/', ' ', $text);
    return iconv('ISO-8859-15', 'UTF-8', $text);
}

?>

Note: the solution provided by this function is not optimal. It removes other characters and not only emojis. I’ve explained that in French: Kirby : plugin et controllers | Luce Carević