


Solve the problem of garbled characters when splitting gbk Chinese, split gbk Chinese garbled characters_PHP tutorial
The problem of garbled characters in gbk Chinese is solved. Split gbk Chinese garbled characters
Recently I encountered a magical word "韬(tao)".
The specific process is as follows:
<span>1</span> <span>$list</span> = <span>explode</span>('|', 'abc弢|bc'<span>); </span><span>2</span> <span>var_dump</span>(<span>$list</span>);
Get the result of this segmentation.
Contrary to imagination, the result is actually like this:
<span>array</span>(3<span>) { [</span>0]=> <span>string</span>(4) "<span>abc? [1]=> string(0) </span>""<span> [2]=> string(2) </span>"bc"<span> }</span>
Garbled characters appeared, and an empty element appeared inexplicably.
The reason is that it turns out that the gbk encoding of the character "韬" is 8f7c, and the ASCII of | is 7c, so explode cuts the second ASCII of "韬" as |.
Since it is a double-byte problem, we solved it with mbstring.
Unfortunately, PHP does not have a function like mb_explode. After searching, I found a mb_split.
<span>array</span> mb_split ( <span>string</span> <span>$pattern</span> , <span>string</span> <span>$string</span> [, int <span>$limit</span> = -1 ] )
No place to declare encoding. Take a closer look and you will see that it is encoded through the mb_regex_encoding declaration.
So I wrote the following code:
<span>1</span> mb_regex_encoding('gbk'<span>); </span><span>2</span> <span>$list</span> = mb_split('\|', 'abc弢|bc'<span>); </span><span>3</span> <span>var_dump</span>(<span>$list</span>);
As a result, php reported an error, mb_regex_encoding does not recognize gbk, embarrassing.
Then use it to know:
<span>1</span> mb_regex_encoding('gb2312'<span>); </span><span>2</span> <span>$list</span> = mb_split('\|', 'abc弢|bc'<span>); </span><span>3</span> <span>var_dump</span>(<span>$list</span>);
Result:
<span>array</span>(3<span>) { [</span>0]=> <span>string</span>(4) "<span>abc? [1]=> string(0) </span>""<span> [2]=> string(2) </span>"bc"<span> }</span>
I found that this method is of no use. ,
As for the reason? The word "Tao" is not in the code set of GB2312! ! ! ! ! However, this function does not support the encoding sets (GBK, GB18030) with this character! ! ! ! !
Since this is not easy to use, maybe a universal regular expression is OK. So we got the following code:
<span>1</span> <span>var_dump</span>(<span>preg_match_all</span>('/([^\|])*/', 'abc弢|bc', <span>$matches</span><span>)); </span><span>2</span> <span>var_dump</span>(<span>$matches</span>);
Result:
int(2<span>) </span><span>array</span>(2<span>) { [</span>0]=> <span>array</span>(2<span>) { [</span>0]=> <span>string</span>(4) "<span>abc? [1]=> string(2) </span>"bc"<span> } [1]=> array(2) { [0]=> string(1) </span>"?<span> [</span>1]=> <span>string</span>(1) "c"<span> } }</span>
Okay, I thought too much.
Now let’s study how to describe this scene using regular expressions.
For reference, Brother Bird’s blog: How to solve the problem of garbled characters when splitting GBK into Chinese. Unfortunately, I, who have relatively low regular expression ability, still can't think of a suitable regular expression (if there are masters who have come up with this regular expression, I hope they can tell me).
I have no choice but to use substr after much thought:
<span> 1</span> <span>function</span> mb_explode(<span>$delimiter</span>, <span>$string</span>, <span>$encoding</span> = <span>null</span><span>){ </span><span> 2</span> <span>$list</span> = <span>array</span><span>(); </span><span> 3</span> <span>is_null</span>(<span>$encoding</span>) && <span>$encoding</span> =<span> mb_internal_encoding(); </span><span> 4</span> <span>$len</span> = mb_strlen(<span>$delimiter</span>, <span>$encoding</span><span>); </span><span> 5</span> <span>while</span>(<span>false</span> !== (<span>$idx</span> = mb_strpos(<span>$string</span>, <span>$delimiter</span>, 0, <span>$encoding</span><span>))){ </span><span> 6</span> <span>$list</span>[] = mb_substr(<span>$string</span>, 0, <span>$idx</span>, <span>$encoding</span><span>); </span><span> 7</span> <span>$string</span> = mb_substr(<span>$string</span>, <span>$idx</span> + <span>$len</span>, <span>null</span>, <span>$encoding</span><span>); </span><span> 8</span> <span> } </span><span> 9</span> <span>$list</span>[] = <span>$string</span><span>; </span><span>10</span> <span>return</span> <span>$list</span><span>; </span><span>11</span> }
Test code:
<span>1</span> <span>$a</span> = 'abc弢|bc'<span>; </span><span>2</span> <span>3</span> <span>var_dump</span>(mb_explode('|', <span>$a</span>, 'gbk'<span>)); </span><span>4</span> <span>var_dump</span>(mb_explode('bc', <span>$a</span>, 'gbk'<span>)); </span><span>5</span> <span>var_dump</span>(mb_explode('弢', <span>$a</span>, 'gbk'));
Result:
<span>array</span>(2<span>) { [</span>0]=> <span>string</span>(5) "abc弢"<span> [</span>1]=> <span>string</span>(2) "bc"<span> } </span><span>array</span>(3<span>) { [</span>0]=> <span>string</span>(1) "a"<span> [</span>1]=> <span>string</span>(3) "弢|"<span> [</span>2]=> <span>string</span>(0) ""<span> } </span><span>array</span>(2<span>) { [</span>0]=> <span>string</span>(3) "abc"<span> [</span>1]=> <span>string</span>(3) "|bc"<span> }</span>
This way you can get the correct results.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

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.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

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 have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.
