($\,):
()\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.