Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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