php 正则表达式 修饰符,php: 正则表达式模式中可用的模式修饰符

($\,):

()\r,\Rand\v-,buttheyarequiteusefulnevertheless.SomeofficialPCREcontroloptionsandtheirchangescomeinhandytoo-unfortunatelyneither(*ANYCRLF),(*ANY)nor(*CRLF)isdocumentedhereonphp.netatthemoment(althoughtheyseemtobeavailableforover10yearsand5monthsnow),buttheyaredescribedonWikipedia("Newline/linebreakoptions"athttps://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions)andofficialPCRElibrarysite("Newlineconvention"athttp://www.pcre.org/original/doc/html/pcresyntax.html#SEC17)prettywell.Thefunctionalityof\Rappearssomehowdisappointing(withdefaultconfigurationofcompiletimeoption)accordingtophp.netaswellasofficialdescription("Newlinesequences"athttps://www.pcre.org/original/doc/html/pcrepattern.html#newlineseq)whenusedimproperly.

Ahintforthoseofyouwhoaretryingtofightoff(orworkaroundatleast)theproblemofmatchingapatterncorrectlyattheend(oratthebeginning)ofanylineevenwithoutthemultiplelinesmode(/m)ormeta-characterassertions($or^).

?php

//VariousOS-eshavevariousendline(a.k.alinebreak)chars:

//-WindowsusesCR+LF(\r

);

//-LinuxLF(

);

//-OSXCR(\r).

//Andthat'swhysingledollarmetaassertion($)sometimesfailswithmultilinemodifier(/m)mode-possiblebuginPHP5.3.8orjusta"feature"(?)ofdefaultconfigurationoptionformeta-characterassertions(^and$)atcompiletimeofPCRE.

$str

=

"ABCABC

123123\r

defdef\rnopnop\r

890890

QRSQRS\r\r~-_~-_"

;

//C3p0_

$pat3

=

'/\w\R?$/mi'

;

//Somehowdisappointingaccordingtophp.netandpcre.orgwhenusedimproperly

$pat3_2

=

'/\w(?=\R)/i'

;

//Muchbetterwithallowedlookaheadassertion(justtodetectwithoutcapture)withoutmultiline(/m)mode;notethatwithalternativeforendofstring((?=\R|$))itwouldgraball7elementsasexpected,but'/(*ANYCRLF)\w$/mi'ismorestraightforwardinuseanyway

$p

=

preg_match_all

(

$pat3

,

$str

,

$m3

);

$r

=

preg_match_all

(

$pat3_2

,

$str

,

$m4

);

echo$str."

3!!!$pat3($p):".print_r($m3[0],true)."

3_2!!!$pat3_2($r):".print_r($m4[0],true);//Notethedifferencebetweenthetwoveryhelpfulescapesequencesin$pat3and$pat3_2(\R)-forsomeapplicationsatleast.

/*Thecodeaboveresultsinthefollowingoutput:

ABCABC

123123

defdef

nopnop

890890

QRSQRS

~-_~-_

3!!!/\w\R?$/mi(5):Array

(

[0]=C

[1]=3

[2]=p

[3]=0

[4]=_

)

3_2!!!/\w(?=\R)/i(6):Array

(

[0]=C

[1]=3

[2]=f

[3]=p

[4]=0

[5]=S

)

*/

?

Unfortunately,Ihaven'tgotanyaccesstoaserverwiththelatestPHPversion-mylocalPHPis5.3.8andmypublichost'sPHPisversion5.2.17.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值