Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[String] Add a method to strip emojis from strings #48850

Closed
javiereguiluz opened this issue Jan 2, 2023 · 5 comments
Closed

[String] Add a method to strip emojis from strings #48850

javiereguiluz opened this issue Jan 2, 2023 · 5 comments
Milestone

Comments

@javiereguiluz
Copy link
Member

javiereguiluz commented Jan 2, 2023

Description

In #48396 we added an option to remove emojis from slugs:

use Symfony\Component\String\Slugger\AsciiSlugger;

$slugger = new AsciiSlugger();
$slugger = $slugger->withEmoji('strip');

$slug = $slugger->slug('a 馃樅, 馃悎鈥嶁瑳, and a 馃');

I'd like to have this feature for strings too, not only slugs.

Example

use function Symfony\Component\String\u;

$string = u('馃樅 Lorem ipsum 馃悎鈥嶁瑳 dolor sit amet 馃')->stripEmojis()->toString();
// $string = ' Lorem ipsum  dolor sit amet '
@javiereguiluz javiereguiluz added this to the 6.3 milestone Jan 2, 2023
@derrabus
Copy link
Member

derrabus commented Jan 2, 2023

How's the result in your example (a-and-a) different from what the slugger would produce?

@javiereguiluz
Copy link
Member Author

@derrabus sorry! It was a copy+paste mistake. I've just updated the above example.

@derrabus
Copy link
Member

derrabus commented Jan 2, 2023

Thanks. Makes much more sense now. 馃槄

@nicolas-grekas
Copy link
Member

You can use the EmojiTransliterator for that.

@fancyweb
Copy link
Contributor

fancyweb commented Jan 2, 2023

I agree a shortcut method would still be useful, something like:

public function stripEmojis(): static
{
    if (null === (self::$transliterators['emoji-strip'] ?? null)) {
        if (!class_exists(EmojiTransliterator::class)) {
            throw new \LogicException(sprintf('You cannot use the "%s()" method as the "symfony/intl" package is not installed. Try running "composer require symfony/intl".', __METHOD__));
        }

        self::$transliterators['emoji-strip'] = EmojiTransliterator::create('emoji-strip');
    }

    $str = clone $this;
    $str->string = self::$transliterators['emoji-strip']->transliterate($str->string);

    return $str;
}

@fabpot fabpot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants