תÒåÐòÁÐ(·´Ð±Ïß)

·´Ð±ÏßÓжàÖÖÓ÷¨¡£Ê×ÏÈ£¬Èç¹û½ô½Ó×ÅÊÇÒ»¸ö·Ç×ÖĸÊý×Ö×Ö·û£¬±íÃ÷È¡Ïû ¸Ã×Ö·ûËù´ú±íµÄÌØÊ⺭Òå¡£ÕâÖÖ½«·´Ð±Ïß×÷ΪתÒå×Ö·ûµÄÓ÷¨ÔÚ×Ö·ûÀà ÄÚ²¿ºÍÍⲿ¶¼¿ÉÓá£

±ÈÈ磬Èç¹ûÄãÏ£ÍûÆ¥ÅäÒ»¸ö "*" ×Ö·û£¬¾ÍÐèÒªÔÚģʽÖÐдΪ "\*"¡£ ÕâÊÊÓÃÓÚÒ»¸ö×Ö·ûÔÚ²»½øÐÐתÒå»áÓÐÌØÊ⺬ÒåµÄÇé¿öÏ¡£ µ«ÊÇ£¬ ¶ÔÓÚ·ÇÊý×Ö×ÖĸµÄ×Ö·û£¬×ÜÊÇÔÚÐèÒªÆä½øÐÐÔ­ÎÄÆ¥ÅäµÄʱºòÔÚËüÇ°ÃæÔö¼ÓÒ»¸ö·´Ð±Ïߣ¬ À´ÉùÃ÷Ëü´ú±í×Ô¼º£¬ÕâÊÇ°²È«µÄ¡£Èç¹ûҪƥÅäÒ»¸ö·´Ð±Ïߣ¬ÄÇôÔÚģʽÖÐʹÓà "\\"¡£

Note:

·´Ð±ÏßÔÚµ¥ÒýºÅºÍË«ÒýºÅ PHP ×Ö·û´® Öж¼ÓÐÌØÊ⺬Òå¡£Òò´ËҪƥÅäÒ»¸ö·´Ð±Ïß \£¬ÕýÔò±í´ïʽд·¨ÊÇ \\£¬ È»ºó PHP ´úÂëÖÐÐèҪתÒåд³É "\\\\" »ò '\\\\'¡£

Èç¹ûÒ»¸öģʽ±»Ê¹Óà PCRE_EXTENDED Ñ¡Ïî±àÒ룬 ģʽÖеĿհ××Ö·û(³ýÁË×Ö·ûÀàÖеÄ)ºÍδתÒåµÄ#µ½ÐÐÄ©µÄËùÓÐ×Ö·û¶¼»á±»ºöÂÔ¡£ ÒªÔÚÕâÖÖÇé¿öÏÂʹÓÿհ××Ö·û»òÕß#£¬¾ÍÐèÒª¶ÔÆä½øÐÐתÒå¡£

·´Ð±ÏߵĵڶþÖÖÓÃ;ÌṩÁËÒ»ÖÖ¶Ô·Ç´òÓ¡×Ö·û½øÐпɼû±àÂëµÄ¿ØÖÆÊֶΡ£ ³ýÁ˶þ½øÖÆµÄ 0 »áÖÕ½áÒ»¸öģʽÍ⣬²¢²»»áÑϸñµÄÏÞÖÆ·Ç´òÓ¡×Ö·û(×ÔÉí)µÄ³öÏÖ£¬ µ«Êǵ±Ò»¸öģʽÒÔÎı¾±à¼­Æ÷µÄ·½Ê½±à¼­×¼±¸µÄʱºò£¬ ʹÓÃÏÂÃæµÄתÒåÐòÁÐÏà±ÈʹÓöþ½øÖÆ×Ö·û»á¸ü¼ÓÈÝÒ×£º

\a
ÏìÁå×Ö·û(Ê®Áù½øÖÆ 07)
\cx
"control-x"£¬x ÊÇÈÎÒâ×Ö·û
\e
תÒå (Ê®Áù½øÖÆ 1B)
\f
»»Ò³ (Ê®Áù½øÖÆ 0C)
\n
»»ÐÐ (Ê®Áù½øÖÆ 0A)
\p{xx}
Ò»¸ö·ûºÏ xx ÊôÐÔµÄ×Ö·û£¬Ïêϸ²é¿´unicode properties ÊôÐÔ
\P{xx}
Ò»¸ö²»·ûºÏxxÊôÐÔµÄ×Ö·û£¬Ïêϸ²é¿´unicode properties ÊôÐÔ
\r
»Ø³µ (Ê®Áù½øÖÆ 0D)
\R
»»Ðзû£ºÄÜÆ¥Åä \n¡¢\r¡¢\r\n
\t
ˮƽÖƱí·û tab (Ê®Áù½øÖÆ 09)
\xhh
hh Ê®Áù½øÖƱàÂëµÄ×Ö·û
\ddd
ddd°Ë½øÖƱàÂëµÄ×Ö·û£¬»òÕߺóÏòÒýÓÃ

\cxµÄÈ·ÇÐЧ¹ûÈçÏ£º Èç¹ûxÊÇÒ»¸öСд×Öĸ£¬Ëü±»×ª»»Îª´óд¡£½Ó×Å£¬ ½«×Ö·ûµÄµÚ6λ(Ê®Áù½øÖÆ 40£¬ÓÒÊýµÚÒ»¸öλΪµÚ0λ)È¡·´¡£ ±ÈÈç\cz³ÉΪʮÁù½øÖƵÄ1A£¬\c{³ÉΪʮÁù½øÖÆ3B£¬ \c;³ÉΪʮÁù½øÖÆ7B¡£

ÔÚ"\x"ºóÃ棬¶ÁÈ¡Á½¸öÊ®Áù½øÖÆÊý(×Öĸ¿ÉÒÔÊÇ´óд»òСд)¡£ ÔÚUTF-8ģʽ£¬ "\x{...}"ÔÊÐíʹÓ㬠»¨À¨ºÅÄÚµÄÄÚÈÝÊÇÊ®Áù½øÖÆÓÐЧÊý×Ö¡£ Ëü½«¸ø³öµÄÊ®Áù½øÖÆÊý×Ö½âÊÍΪ UTF-8 ×Ö·û´úÂë¡£Ô­À´µÄÊ®Áù½øÖÆתÒåÐòÁУ¬ \xhh£¬ Æ¥ÅäÒ»¸öË«×Ö½ÚµÄUTF-8×Ö·û£¬Èç¹ûËüµÄÖµ´óÓÚ127

ÔÚ"\0"Ö®ºó£¬ ¶ÁÈ¡Á½¸ö°Ë½øÖÆÊý¡£ËùÓÐÇé¿öÏ£¬Èç¹ûÊýÉÙÓÚ2¸ö£¬ÔòÖ±½ÓʹÓᣠÐòÁÐ "\0\x\07" Ö¸¶¨ÁËÁ½¸ö¶þ½øÖÆ 0 ½ô¸ú×ÅÒ»¸ö BEL ×Ö·û¡£ ÇëÈ·±£³õʼµÄ 0 Ö®ºóµÄÁ½¸öÊý×ÖÊǺϷ¨µÄ°Ë½øÖÆÊý¡£

´¦ÀíÒ»¸ö·´Ð±Ïß½ô¸ú×ŵIJ»ÊÇ0µÄÊý×ÖµÄÇé¿ö±È½Ï¸´ÔÓ¡£ÔÚ×Ö·ûÀàÍⲿ£¬ PCRE ¶ÁÈ¡Ëü²¢ÒÔÊ®½øÖƶÁÈ¡½ôËæÆäºóµÄÊý×Ö¡£ Èç¹ûÊýֵСÓÚ 10£¬ »òÕß֮ǰ²¶»ñµ½Á˸ÃÊý×ÖÄܹ»´ú±íµÄ×óÀ¨ºÅ(×Ó×é)£¬ Õû¸öÊý×ÖÐòÁб»ÈÏΪÊǺóÏòÒýÓ᣺óÏòÒýÓÃÈçºÎ¹¤×÷ÔÚºóÃæÃèÊö£¬ ½ÓÏÂÀ´¾Í»áÌÖÂÛÀ¨ºÅ×Ó×é¡£

ÔÚÒ»¸ö×Ö·ûÀàÀïÃ棬»òÕßÊ®½øÖÆÊý´óÓÚ 9 ²¢ÇÒûÓÐÄÇô¶àµÄ×Ó×é±»²¶»ñ£¬ PCRE ÖØжÁÈ¡·´Ð±ÏߺóµÄµÚÈý¸ö 8 ½øÖÆÊý×Ö£¬²¢ÇÒ´Ó×îµÍµÄ 8 λÉú³Éµ¥×Ö½ÚÖµ¡£ ÈκεĺóÐøÊý×Ö¶¼´ú±íËüÃÇ×ÔÉí¡£ÀýÈ磺

\040
¿Õ¸ñµÄÁíÍâÒ»ÖÖÓ÷¨
\40
µ±ÌṩÁËÉÙÓÚ40¸ö×Ó×éʱҲÈÏΪÊÇ¿Õ¸ñ¡£
\7
ʼÖÕÊǺóÏòÒýÓÃ
\11
¿ÉÄÜÊǺóÏòÒýÓã¬Ò²¿ÉÄÜÊÇÖƱí·û
\011
×ÜÊÇÒ»¸öÖƱí·û
\0113
Ò»¸öÖƱí·û½ô¸ú×ÅÒ»¸ö3(ÒòΪÿ´Î×î¶àÖ»¶ÁÈ¡3¸ö8½øÖÆλ
\113
°Ë½øÖÆ113´ú±íµÄ×Ö·û(since there can be no more than 99 back references)
\377
8½øÖÆ377ÊÇ10½øÖÆ255£¬ Òò´Ë´ú±íÒ»¸öÈ«1µÄ×Ö·û
\81
Ò»¸öºóÏòÒýÓûòÕßÒ»¸ö¶þ½øÖÆ 0 ½ô¸ú×ÅÁ½¸öÊý×Ö 8 ºÍ 1(ÒòΪ8²»ÊÇ8½øÖÆÓÐЧÊý×Ö)

×¢Ò⣬°Ë½øÖÆÖµµÄ 100 »òÕ߸ü´óµÄÖµ±ØÐëûÓÐÇ°ÖõÄ0Òýµ¼£¬ ÒòΪÿ´Î×î¶à¶ÁÈ¡3¸ö8½øÖÆλ.

ËùÓÐÐòÁж¨ÒåµÄµ¥×Ö½ÚÖµ¶¼¿ÉÒÔÔÚ×Ö·ûÀàÄÚ²¿»òÍⲿʹÓá£ÁíÍ⣬ÔÚ×Ö·ûÀàÖÐ, ÐòÁÐ "\b" ½âÊÍΪÍ˸ñ×Ö·û(hex 08)¡£ ×Ö·ûÀàÍâËüÓÖÓв»Í¬µÄÒâÒå(ÏÂÃæÓÐÃèÊö)¡£

·´Ð±ÏߵĵÚÈýÖÖÓ÷¨ÊÇÓÃÀ´ÃèÊöÌض¨µÄ×Ö·ûÀࣺ

\d
ÈÎÒâÊ®½øÖÆÊý×Ö
\D
ÈÎÒâ·ÇÊ®½øÖÆÊý×Ö
\h
ÈÎÒâˮƽ¿Õ°××Ö·û
\H
ÈÎÒâ·Çˮƽ¿Õ°××Ö·û
\s
ÈÎÒâ¿Õ°××Ö·û
\S
ÈÎÒâ·Ç¿Õ°××Ö·û
\v
ÈÎÒâ´¹Ö±¿Õ°××Ö·û
\V
ÈÎÒâ·Ç´¹Ö±¿Õ°××Ö·û
\w
ÈÎÒâµ¥´Ê×Ö·û
\W
ÈÎÒâ·Çµ¥´Ê×Ö·û

ÉÏÃæÿһ¶ÔתÒåÐòÁж¼´ú±íÁËÍêÕû×Ö·û¼¯ÖÐÁ½¸ö²»ÏཻµÄ²¿·Ö£¬ ÈÎÒâ×Ö·ûÒ»¶¨»áÆ¥ÅäÆäÖÐÒ»¸ö£¬Í¬Ê±Ò»¶¨²»»áÆ¥ÅäÁíÍâÒ»¸ö¡£

"¿Õ°××Ö·û"£¨whitespace£©ÊÇ HT (9)¡¢LF (10)¡¢FF (12)¡¢CR (13)¡¢space (32)¡£ È»¶ø£¬Èô·¢ÉúÁ˱¾µØ»¯Æ¥Å䣬ÔÚ´úÂëµã 128-255 ·¶Î§ÄÚÒà¿ÉÄܳöÏÖ¿Õ°××Ö·û£¬ ±ÈÈç˵ NBSP (A0)¡£

µ¥´Ê×Ö·ûÖ¸µÄÊÇÈÎÒâ×Öĸ¡¢Êý×Ö¡¢Ï»®Ïß¡£ Ò²¾ÍÊÇ˵ÈÎÒâ¿ÉÒÔ×é³Éperlµ¥´ÊµÄ×Ö·û¡£ ×ÖĸºÍÊý×ֵĶ¨Òåͨ¹ýPCRE×Ö·û±í¿ØÖÆ£¬¿ÉÒÔͨ¹ýÖ¸¶¨µØÓòÉèÖÃʹÆäÆ¥Åä¸Ä±ä¡£±ÈÈ磬 ÔÚ·¨¹ú (fr) ±¾µØ»¯ÉèÖÃÖУ¬Ò»Ð©³¬¹ý 128 µÄ×Ö·û´úÂë±»ÓÃÓÚÖØÒô×Öĸ£¬ ËüÃÇ¿ÉÒÔʵÓà \w Æ¥Åä¡£

ÕâЩ×Ö·ûÀàÐòÁÐÔÚ×Ö·ûÀàÄÚ²¿»òÍⲿ¶¼¿ÉÒÔ³öÏÖ¡£ ËûÃÇÿ´ÎÆ¥ÅäËù´ú±íµÄ×Ö·ûÀàÐÍÖеÄÒ»¸ö×Ö·û¡£ Èç¹ûµ±Ç°Æ¥ÅäµãλÓÚÄ¿±ê×Ö·û´®Ä©Î²£¬ ËüÃÇÖеÄËùÓÐ×Ö·û¶¼Æ¥Åäʧ°Ü£¬ ÒòΪûÓÐ×Ö·ûÈÃËüÃÇÆ¥ÅäÁË¡£

·´Ð±ÏߵĵÚËÄÖÖÓ÷¨ÊÇһЩ¼òµ¥µÄ¶ÏÑÔ¡£ Ò»¸ö¶ÏÑÔÖ¸¶¨Ò»¸ö±ØÐëÔÚÌض¨Î»ÖÃÆ¥ÅäµÄÌõ¼þ£¬ ËüÃDz»»á´ÓÄ¿±ê×Ö·û´®ÖÐÏûºÄÈκÎ×Ö·û¡£ ½ÓÏÂÀ´ÎÒÃÇ»áÌÖÂÛʹÓÃ×Ó×éµÄ¸ü¼Ó¸´ÔӵĶÏÑÔ¡£ ·´Ð±Ï߶ÏÑÔ°üÀ¨£º

\b
µ¥´Ê±ß½ç
\B
·Çµ¥´Ê±ß½ç
\A
Ä¿±êµÄ¿ªÊ¼Î»ÖÃ(¶ÀÁ¢ÓÚ¶àÐÐģʽ)
\Z
Ä¿±êµÄ½áÊøλÖûò½áÊø´¦µÄ»»Ðзû(¶ÀÁ¢ÓÚ¶àÐÐģʽ)
\z
Ä¿±êµÄ½áÊøλÖÃ(¶ÀÁ¢ÓÚ¶àÐÐģʽ)
\G
ÔÚÄ¿±êÖÐÊ×´ÎÆ¥ÅäλÖÃ

ÕâЩ¶ÏÑÔ²»ÄܳöÏÖÔÚ×Ö·ûÀàÖÐ(µ«ÊÇ×¢Ò⣬ "\b"ÔÚ×Ö·ûÀàÖÐÓв»Í¬µÄÒâÒ壬 ±íʾµÄÊÇÍ˸ñ(backspace)×Ö·û)

Ò»¸öµ¥´Ê±ß½ç±íʾµÄÊÇÔÚÄ¿±ê×Ö·û´®ÖУ¬ µ±Ç°×Ö·ûºÍÇ°Ò»¸ö×Ö·û²»Í¬Ê±Æ¥Åä\w»ò\W(Ò»¸öÆ¥Åä\w, Ò»¸öÆ¥Åä\W)£¬ »òÕß×÷Ϊ×Ö·û´®¿ªÊ¼»ò½áβ×Ö·ûµÄʱºòµ±Ç°×Ö·ûÆ¥Åä\w¡£

\A£¬ \Z£¬ \z¶ÏÑÔ²»Í¬ÓÚ´«Í³µÄ^ºÍ$(Ïê¼û ê)£¬ ÒòΪËûÃÇÓÀԶƥÅäÄ¿±ê×Ö·û´®µÄ¿ªÊ¼ºÍ½á⣬¶ø²»»áÊÜģʽÐÞÊηûµÄÏÞÖÆ¡£ ËüÃDz»ÊÜPCRE_MULTILINE£¬PCRE_DOLLAR_ENDONLYÑ¡ÏîµÄÓ°Ïì¡£ \Z ºÍ \z Ö®¼äµÄ²»Í¬ÔÚÓÚµ±×Ö·û´®½áÊø×Ö·ûʱ»»Ðзûʱ \Z »á½«Æä¿´×ö×Ö·û´®½áβƥÅä, ¶ø \z ֻƥÅä×Ö·û´®½áβ¡£

\G ¶ÏÑÔÔÚÖ¸¶¨ÁËoffset ²ÎÊýµÄpreg_match() µ÷ÓÃÖУ¬ ½öÔÚµ±Ç°Æ¥ÅäλÖÃÔÚÆ¥Å俪ʼµãµÄʱºò²ÅÊdzɹ¦µÄ¡£ µ± offset µÄÖµ²»Îª 0 µÄʱºò£¬ ËüÓë \A ÊDz»Í¬µÄ¡£ Òë×¢£ºÁíÍâÒ»µãÓë \A µÄ²»Í¬Ö®´¦ÔÚÓÚʹÓà preg_match_all() ʱ£¬ ÿ´ÎÆ¥Åä \G Ö»ÊǶÏÑÔÊÇ·ñÊÇÆ¥Åä½á¹ûµÄ¿ªÊ¼Î»Ö㬠¶ø \A ¶ÏÑÔµÄÔòÊÇÆ¥Åä½á¹ûµÄ¿ªÊ¼Î»ÖÃÊÇ·ñÔÚÄ¿±ê×Ö·û´®¿ªÊ¼Î»Öá£

\Q ºÍ \E ¿ÉÒÔÓÃÓÚÔÚģʽÖкöÂÔÕýÔò±í´ïʽԪ×Ö·û¡£±ÈÈ磺 \w+\Q.$.\E$ »áÆ¥ÅäÒ»¸ö»ò¶à¸öµ¥´Ê×Ö·û£¬½ô½Ó×Å .$.£¬ ×îºóêÏò×Ö·û´®Ä©Î²¡£

\K ¿ÉÒÔÓÃÓÚÖØÖÃÆ¥Åä¡£ ±ÈÈ磬 foot\Kbar Æ¥Åä"footbar"¡£ µ«Êǵõ½µÄÆ¥Åä½á¹ûÊÇ "bar"¡£µ«ÊÇ£¬ \K µÄʹÓò»»á¸ÉÔ¤µ½×Ó×éÄÚµÄÄÚÈÝ£¬ ±ÈÈç (foot)\Kbar Æ¥Åä "footbar"£¬µÚÒ»¸ö×Ó×éÄڵĽá¹ûÈÔÈ»»áÊÇ "foo"¡£Òë×¢£º \K ·ÅÔÚ×Ó×éºÍ×Ó×éÍâÃæµÄЧ¹ûÊÇÒ»ÑùµÄ¡£

User Contributed Notes

tharabar at gmail dot com 19-Sep-2019 04:08
Required to use \007 instead of \a
Anonymous 25-Jul-2019 03:03
A non breaking space is not considered as a space and cannot be caught by \s.

it can be found with :

- [\xc2\xa0] in utf-8
- \x{00a0} in unicode
Wirek 22-Feb-2018 01:38
Significantly updated version (with new $pat4 utilising \R properly, its results and comments):
Note that there are (sometimes difficult to grasp at first glance) nuances of meaning and application of escape sequences like \r, \R and \v - none of them is perfect in all situations, but they are quite useful nevertheless. Some official PCRE control options and their changes come in handy too - unfortunately neither (*ANYCRLF), (*ANY) nor (*CRLF) is documented here on php.net at the moment (although they seem to be available for over 10 years and 5 months now), but they are described on Wikipedia ("Newline/linebreak options" at https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions) and official PCRE library site ("Newline convention" at http://www.pcre.org/original/doc/html/pcresyntax.html#SEC17) pretty well. The functionality of \R appears somehow disappointing (with default configuration of compile time option) according to php.net as well as official description ("Newline sequences" at https://www.pcre.org/original/doc/html/pcrepattern.html#newlineseq) when used improperly.
A hint for those of you who are trying to fight off (or work around at least) the problem of matching a pattern correctly at the end ($) of any line in multiple lines mode (/m).
<?php
// Various OS-es have various end line (a.k.a line break) chars:
// - Windows uses CR+LF (\r\n);
// - Linux LF (\n);
// - OSX CR (\r).
// And that's why single dollar meta assertion ($) sometimes fails with multiline modifier (/m) mode - possible bug in PHP 5.3.8 or just a "feature"(?).
$str="ABC ABC\n\n123 123\r\ndef def\rnop nop\r\n890 890\nQRS QRS\r\r~-_ ~-_";
//          C          3                   p          0                   _
$pat1='/\w$/mi';    // This works excellent in JavaScript (Firefox 7.0.1+)
$pat2='/\w\r?$/mi';    // Slightly better
$pat3='/\w\R?$/mi';    // Somehow disappointing according to php.net and pcre.org when used improperly
$pat4='/\w(?=\R)/i';    // Much better with allowed lookahead assertion (just to detect without capture) without multiline (/m) mode; note that with alternative for end of string ((?=\R|$)) it would grab all 7 elements as expected
$pat5='/\w\v?$/mi';
$pat6='/(*ANYCRLF)\w$/mi';    // Excellent but undocumented on php.net at the moment (described on pcre.org and en.wikipedia.org)
$n=preg_match_all($pat1, $str, $m1);
$o=preg_match_all($pat2, $str, $m2);
$p=preg_match_all($pat3, $str, $m3);
$r=preg_match_all($pat4, $str, $m4);
$s=preg_match_all($pat5, $str, $m5);
$t=preg_match_all($pat6, $str, $m6);
echo
$str."\n1 !!! $pat1 ($n): ".print_r($m1[0], true)
    .
"\n2 !!! $pat2 ($o): ".print_r($m2[0], true)
    .
"\n3 !!! $pat3 ($p): ".print_r($m3[0], true)
    .
"\n4 !!! $pat4 ($r): ".print_r($m4[0], true)
    .
"\n5 !!! $pat5 ($s): ".print_r($m5[0], true)
    .
"\n6 !!! $pat6 ($t): ".print_r($m6[0], true);
// Note the difference among the three very helpful escape sequences in $pat2 (\r), $pat3 and $pat4 (\R), $pat5 (\v) and altered newline option in $pat6 ((*ANYCRLF)) - for some applications at least.

/* The code above results in the following output:
ABC ABC

123 123
def def
nop nop
890 890
QRS QRS

~-_ ~-_
1 !!! /\w$/mi (3): Array
(
    [0] => C
    [1] => 0
    [2] => _
)

2 !!! /\w\r?$/mi (5): Array
(
    [0] => C
    [1] => 3
    [2] => p
    [3] => 0
    [4] => _
)

3 !!! /\w\R?$/mi (5): Array
(
    [0] => C

    [1] => 3
    [2] => p
    [3] => 0
    [4] => _
)

4 !!! /\w(?=\R)/i (6): Array
(
    [0] => C
    [1] => 3
    [2] => f
    [3] => p
    [4] => 0
    [5] => S
)

5 !!! /\w\v?$/mi (5): Array
(
    [0] => C

    [1] => 3
    [2] => p
    [3] => 0
    [4] => _
)

6 !!! /(*ANYCRLF)\w$/mi (7): Array
(
    [0] => C
    [1] => 3
    [2] => f
    [3] => p
    [4] => 0
    [5] => S
    [6] => _
)
 */
?>
Unfortunately, I haven't got any access to a server with the latest PHP version - my local PHP is 5.3.8 and my public host's PHP is version 5.2.17.
Wirek 20-Feb-2018 12:21
Note that there are (sometimes difficult to grasp at first glance) nuances of meaning and application of escape sequences like \r, \R and \v - none of them is perfect in all situations, but they are quite useful nevertheless. Some official PCRE control options and their changes come in handy too - unfortunately neither (*ANYCRLF), (*ANY) nor (*CRLF) is documented here on php.net at the moment (although they seem to be available for over 10 years and 5 months now), but they are described on Wikipedia ("Newline/linebreak options" at https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions) and official PCRE library site ("Newline convention" at http://www.pcre.org/original/doc/html/pcresyntax.html#SEC17) pretty well. The functionality of \R appears somehow disappointing (with default configuration of compile time option) according to php.net as well as official description ("Newline sequences" at https://www.pcre.org/original/doc/html/pcrepattern.html#newlineseq).

A hint for those of you who are trying to fight off (or work around at least) the problem of matching a pattern correctly at the end ($) of any line in multiple lines mode (/m).
<?php
// Various OS-es have various end line (a.k.a line break) chars:
// - Windows uses CR+LF (\r\n);
// - Linux LF (\n);
// - OSX CR (\r).
// And that's why single dollar meta assertion ($) sometimes fails with multiline modifier (/m) mode - possible bug in PHP 5.3.8 or just a "feature"(?).
$str="ABC ABC\n\n123 123\r\ndef def\rnop nop\r\n890 890\nQRS QRS\r\r~-_ ~-_";
//          C          3                   p          0                   _
$pat1='/\w$/mi';    // This works excellent in JavaScript (Firefox 7.0.1+)
$pat2='/\w\r?$/mi';
$pat3='/\w\R?$/mi';    // Somehow disappointing according to php.net and pcre.org
$pat4='/\w\v?$/mi';
$pat5='/(*ANYCRLF)\w$/mi';    // Excellent but undocumented on php.net at the moment
$n=preg_match_all($pat1, $str, $m1);
$o=preg_match_all($pat2, $str, $m2);
$p=preg_match_all($pat3, $str, $m3);
$r=preg_match_all($pat4, $str, $m4);
$s=preg_match_all($pat5, $str, $m5);
echo
$str."\n1 !!! $pat1 ($n): ".print_r($m1[0], true)
    .
"\n2 !!! $pat2 ($o): ".print_r($m2[0], true)
    .
"\n3 !!! $pat3 ($p): ".print_r($m3[0], true)
    .
"\n4 !!! $pat4 ($r): ".print_r($m4[0], true)
    .
"\n5 !!! $pat5 ($s): ".print_r($m5[0], true);
// Note the difference among the three very helpful escape sequences in $pat2 (\r), $pat3 (\R), $pat4 (\v) and altered newline option in $pat5 ((*ANYCRLF)) - for some applications at least.

/* The code above results in the following output:
ABC ABC

123 123
def def
nop nop
890 890
QRS QRS

~-_ ~-_
1 !!! /\w$/mi (3): Array
(
    [0] => C
    [1] => 0
    [2] => _
)

2 !!! /\w\r?$/mi (5): Array
(
    [0] => C
    [1] => 3
    [2] => p
    [3] => 0
    [4] => _
)

3 !!! /\w\R?$/mi (5): Array
(
    [0] => C

    [1] => 3
    [2] => p
    [3] => 0
    [4] => _
)

4 !!! /\w\v?$/mi (5): Array
(
    [0] => C

    [1] => 3
    [2] => p
    [3] => 0
    [4] => _
)

5 !!! /(*ANYCRLF)\w$/mi (7): Array
(
    [0] => C
    [1] => 3
    [2] => f
    [3] => p
    [4] => 0
    [5] => S
    [6] => _
)
 */
?>
Unfortunately, I haven't got any access to a server with the latest PHP version - my local PHP is 5.3.8 and my public host's PHP is version 5.2.17.
vea dot git at gmail dot com 12-Jul-2017 03:36
\b BS

$str=""

$str =str_replace("\b", "", $str);
//echo 

$str =str_replace(chr(8), "", $str);
//echo

$str =str_replace("\\b", "", $str);
//echo
error17191 at gmail dot com 21-Sep-2015 12:55
Some escape sequence like the tab character \t won't work inside single quotes '\t', But they work inside double quotes.
Other escape sequences like the backspace character won't work unless you use its ascii codepoint and chr() function i.e. chr(8)
info at maisuma dot jp 17-Jun-2014 04:47
You can use Unicode character escape sequences (tested on PHP 5.3.3 & PCRE 7.8).

<?php
//This source is supposed to be written in UTF-8.
$a='€';
var_dump(preg_match('/\\x{20ac}/u',$a)); //Match!
bluemoehre at gmx dot de 14-Jan-2014 12:06
Using \R in character classes is NOT possible:

var_dump( preg_match('#\R+#',"\n") ); -> int(1)
var_dump( preg_match('#[\R]+#',"\n") ); -> int(0)
collons at ya dot com 04-Oct-2013 04:06
The pattern "/\\A/" may be replaced by "/\\\A/" in order to match a "\A" string. Any other escaped "\" looks to work fine so you can use "/\\S/", for instance, to match a "\S" string.
mike at eastghost dot com 28-Mar-2012 07:25
"line break" is ill-defined:

 -- Windows uses CR+LF (\r\n)
 -- Linux LF (\n)
 -- OSX CR (\r)

Little-known special character:
\R in preg_* matches all three.

preg_match( '/^\R$/', "match\nany\\n\rline\r\nending\r" ); // match any line endings
grigor at the domain gatchev.info 22-Jun-2011 01:59
As \v matches both single char line ends (CR, LF) and double char (CR+LF, LF+CR), it is not a fixed length atom (eg. is not allowed in lookbehind assertions).