字符串的自然排序问题
1、
$a = 'x2-g8';
$b = 'x2-y7';
echo strnatcasecmp($a, $b); //输出-1
---------------------------------------------
2、
$a = 'a10a';
$b = 'a010';
echo strnatcasecmp($a, $b); //输出1
---------------------------------------------
3、
$a = '10a';
$b = '10.0a';
echo strnatcasecmp($a, $b); //输出1
---------------------------------------------
问题:
1、手册中写的是:
Strings can contain several number parts: x2-g8 in which case numeric fields are separated by nonnumeric characters. Leading spaces are ignored.
这意思不就是说可以忽略前导位置吗?那$a和$b不就是28,27吗?
2、$a和$b不都是10吗?为什么会被认为不同呢?
3、我还是认为$a和$b都是10啊~
看了一遍手册中的“字符串自然排序”规则,但是没太看懂,能帮我讲一下字符串自然排序的规则有哪些吗?
另外字符串自然排序和数组用的自然排序(natcasesort())是一个规则吗?
谢谢!
------解决方案--------------------
没用过!你可以用数组排序,那就是自然排序!
一般是根据拉字字母排序!
中文,可以转成gbk然后排序。转成相应的语言码后, 应可以直接比较大小。
大小写字母是不同的。
------解决方案--------------------
#1.字符串首位为0将被忽略,所以你看下面两个例子是不同的的结果
echo strnatcasecmp('7','08'); //-1
echo strnatcasecmp('y7','y08'); //1
#2.字符串内的空格将被忽略
#3.如果是非数字对上数字,或者非数字对非数字,则以ascii码为准,如果是数字对数字,有两种情况
a).任意一方数字串以0开头被视为小的一方,比如
echo strnatcasecmp('y721','y07203'); //1,因为y=y,7 > 0
b).如果都非0,则数字小的一方为小的一方,比如
echo strnatcasecmp('y723','y70212'); //1,因为y=y,70212 > 723
可以阅读源代码,这部分很容易看懂。
------解决方案--------------------
echo strnatcasecmp('7','08'); //-1
echo strnatcasecmp('y7','y08'); //1
-11
$a = 'x2-g8';
$b = 'x2-y7';
echo strnatcasecmp($a, $b); //输出-1
---------------------------------------------
2、
$a = 'a10a';
$b = 'a010';
echo strnatcasecmp($a, $b); //输出1
---------------------------------------------
3、
$a = '10a';
$b = '10.0a';
echo strnatcasecmp($a, $b); //输出1
-111
有什么不对的吗?
字符串比较时时按字节比较,直至对应位不等时,比较相应两字符
如果说明文字与此解释不符,请注意一个事实:老外不是上帝
如果只是理解上的差异,请去打你英语老师的屁股

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

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

Dreamweaver Mac version
Visual web development tools

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
