search
HomeBackend DevelopmentPHP TutorialIntroduction to a class library that extends PHP-based Emoji processing methods

Introduction to CarmelaCarmela provides a set of solutions for processing 4-section UTF-8 based on PHP, PHP extensions, JAVA, C++ and other languages, such as common Emoji tag supportBackground: UTF-8 format Strings containing Emoji expressions are directly inserted into the database. If the database has not been adjusted, an error will be reported. This problem can be avoided by changing the character set of the database and table to utf8mb4_general_ci. However, in many large-scale systems and architectures, modifying the character set of the database may cause many problems, such as PC-side display and compatibility issues between new and old data. For this kind of problem, there is another solution, which is to replace before entering the database and do reverse replacement according to the client type after leaving the database. CarmelaCarmela provides a solution for processing 4-section UTF-8 based on PHP extension, which can replace UTF-8 characters larger than 3 bytes in UTF-8 into UBB mode, such as a certain UTF-8 character % f0%9f%91%a4 (for the convenience of display, the encode mode of the emoji tag is shown), what it looks like after replacement [u]1f464[/u], and when reading from the database, according to different request clients (iOS, Andriod , PC) do reverse substitution. The name Carmela comes from "Different Carmela". The "Different Carmela" series of stories tells the adventure stories of the hen Carmela and her children Carmelido and Carmen. In the Carmela family Everyone is so different, they dare to dream, and they dare to try things that others dare not think of. Installation1. Compile and package git clone https://github.com/ugg/Carmela /phpize ./configure --with-php-c/php-config-path make make install
  • Modify the configuration file

    vim /php.ini

  • Add the following content[carmela] extension=carmela.so Method: carmela_str2ubb: Convert the string containing the emoji tag into ubb mode , what it looks like after replacement [u]1f464[/u]. An example: $str = urldecode("This is test %F0%9F%98%9C+%F0%9F%98%99 by ugg"); echo "str:".$str."\n"; echo "ubb:".carmela_str2ubb($str)."\n"; Output result: str:This is test xxxx(CSDN Emoji不能展示用XXXX代替) by ugg ubb:This is test [u]1f61c[/u] [u]1f619[/u] by ugg carmela_ubb2str: Contains the ubb tag converted to utf-8 string format. For PC platform transfer, you can refer to the carmela_ubb2str method in encode.class.php. An example: $str = urldecode("This is test %F0%9F%98%9C+%F0%9F%98%99 by ugg"); $str = carmela_str2ubb($str); echo "ubb:".$str."\n"; echo "str:".carmela_ubb2str($str)."\n"; Output result: ubb:This is test [u]1f61c[/u] [u]1f619[/u] by ugg str:This is test(CSDN Emoji不能展示用XXXX代替) by uggcarmela_substr: Intercept the specified length of characters from the string containing emoji characters. carmela_sububb: Intercept the specified length of characters from the string containing the ubb tag. carmela_delstr: Delete emoji characters in strings, non-strict mode, 3-byte emoji characters cannot be deleted, mainly used in some. carmela_delubb: Delete ubb tags in strings containing ubb tags. Performance使用PHP分别实现了两种方法,分别使用PHP的str_replace方法和PHP查找四字节emoji,进行替换的方法,以及PHP扩展方式,使用相同数据分别进行测试,测试效果如下。=========================== 方案1:PHP str_replace方式 ========================= =========== EMOJI TO STRING ========== TIME:781.94ms,处理行数: 100,处理字数:10100,处理字节数:31028 平均每行处理时间:7.819ms =========== STRING TO EMOJI ========== TIME:118.566ms,处理行数: 100,处理字数:18710,处理字节数:37793 平均每行处理时间:1.186ms =========================== 方案2:PHP字符查找方式 ========================= =========== EMOJI TO STRING ========== TIME:51.526ms,处理行数: 100,处理字数:10100,处理字节数:31028 平均每行处理时间:0.515ms =========== STRING TO EMOJI ========== TIME:27.959ms,处理行数: 100,处理字数:23092,处理字节数:41236 平均每行处理时间:0.28ms =========================== 方案3:PHP扩展方式 ========================= =========== EMOJI TO STRING ========== TIME:0.721ms,处理行数: 100,处理字数:10100,处理字节数:31028 平均每行处理时间:0.007ms =========== STRING TO EMOJI ========== TIME:0.956ms,处理行数: 100,处理字数:20308,处理字节数:38452 平均每行处理时间:0.01ms 从以上测试效果上来看,str_replace方式,性能非常的差。使用PHP直接编写替换函数方式,性能提升10倍多,而采用扩展方式后,性能提升明显,在把emoji从字符形式转换为ubb方式时,性能提升1000倍。以上测试数据通过create_file.php可以动态生成。本测试用例,生成100行数据,每行100个字符,100字符中可以包含3-10个emoji字符,进行测试的,直接运行benchmark.php 查看运行性能。原理处理四字节的emoji原理非常简单,通过字符对比找到emoji字符进行替换。难点就是在基本原理上如何提升性能,如何快速查找,替换。PHP扩展方式,为大家提供了一种思路,可以参考这种思路实现java,C#,js等等版本的。PC如何支持EMoji表情展示?在项目目录中的emoji目录下找到images目录,从web根目录创建emoji文件夹,把images文件夹整个拷贝到emoji文件下,调用encode.class.php里面的carmela_ubb2str方法,Util_Encode::carmela_ubb2str($str, "PC"); 即可在PC上展示Emoji表情,目前收集到的845个emoji表情,一些新的表情符号并没有纳入其中,当然,目前这种方法并没有写入PHP扩展中,性能相对来说并不高。Contact ugg.xchj@gmail.com for all questions

    以上就介绍了基于PHP扩展一种处理Emoji方法的类库介绍,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

    In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

    PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

    PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    Will R.E.P.O. Have Crossplay?
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor