Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement ucs2 coding with php

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you php how to achieve ucs2 coding, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Php to achieve ucs2 coding methods: 1, to create a PHP example file; 2, through the "function Ucs2Code ($str,$encode=" UTF-8 ") {.}" method to achieve ucs2 coding.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.

PHP implements UCS2 Encoding and Decoding Encoding when sending SMS messages

The code is as follows:

/ * @ Method Ucs2Code UCS2 Encoding * @ Param $str input string * @ Param $encod input string Encoding Type (UTF-8,GB2312,GBK) * @ Return returns the encoded string * / function Ucs2Code ($str,$encode= "UTF-8") {$jumpbit=strtoupper ($encode) = 'GB2312'?2:3;// hops $strlen=strlen ($str); / / string length $pos=0;// position $buffer=array (); for ($pos=0) Pos=0xa1) {/ / 0xa1 (161Chinese character coding starts $tmpChar=substr ($str,$pos,$jumpbit); $pos+=$jumpbit;} else {$tmpChar=substr ($str,$pos,1); + + $pos;} $buffer [] = bin2hex (iconv ("UTF-8", "UCS-2", $tmpChar));} return strtoupper (join ("", $buffer)) } / * @ Method unUcs2Code UCS2 Decoding * @ Param $str input string * @ Param $encod input string Encoding Type (UTF-8,GB2312,GBK) * @ Return returns the decoded string * / function unUcs2Code ($str,$encode= "UTF-8") {$strlen=strlen ($str); $step=4;$buffer=array (); for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report