Home > Backend Development > PHP Tutorial > Introduction to a class library that extends PHP-based Emoji processing methods

Introduction to a class library that extends PHP-based Emoji processing methods

WBOY
Release: 2016-08-08 09:22:41
Original
992 people have browsed it
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教程有兴趣的朋友有所帮助。

    Related labels:
    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template