


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.

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
