John Davidson

Detect and replace emojis in a string with php

0 comments
Message:


I use this preg replace to remove all emojis in a string:


$data['message'] = preg_replace("/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u", "", $data['message']); 

This works very well but I dont want to remove them. Instead I have to replace them with a bbcode. That means every emojis in a string should replaced with a own bbcode.


Example:


U+1F600

becomes


[emoji]1f600[/emoji]

or


U+1F603

becomes


[emoji]1f603[/emoji]

Is this possible? Thank you very much.



My Answer:
'/[\x{1F600}-\x{1F64F}]/u', '', $data['message']);

This code uses a regular expression to match emojis in the range of U+1F600 to U+1F64F and replaces them with an empty string. You can adjust the range to include more emojis if needed.

Rate this post

5 of 5 based on 3118 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us