Guest User

Untitled

a guest
Feb 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. $subject = 'abc#
  2. 123#
  3. ';
  4. $pattern = '/1...$/';
  5. preg_match_all($pattern,$subject,$matches); // no match
  6.  
  7. $pattern = '/1...(.)$/';
  8.  
  9. echo bin2hex($matches[1]); // 28
  10.  
  11. $subject = 'abc#
  12. 123#
  13. ';
  14. $pattern = '/1...(.)$/';
  15. preg_match_all($pattern,$subject,$matches);
  16. echo bin2hex($matches[1]); // 28
  17. // 28 is equivalent of r or CR(carriage return)
  18.  
  19. /1...$/m
  20.  
  21. <?php
  22. // Various OS-es have various end line (a.k.a line break) chars:
  23. // - Windows uses CR+LF (rn);
  24. // - Linux LF (n);
  25. // - OSX CR (r).
  26. // And that's why single dollar meta assertion ($) sometimes fails with multiline modifier (/m) mode - possible bug in PHP 5.3.8(?).
  27. $str="ABC ABCnn123 123rndef defrnop noprn890 890nQRS QRSrr~-_ ~-_";
  28. // C 3 p 0 _
  29. $pat1='/w$/mi';
  30. $pat2='/wr?$/mi';
  31. $pat3='/wR?$/mi';
  32. $pat4='/wv?$/mi';
  33. $n=preg_match_all($pat1, $str, $m1);
  34. $o=preg_match_all($pat2, $str, $m2);
  35. $p=preg_match_all($pat3, $str, $m3);
  36. $r=preg_match_all($pat4, $str, $m4);
  37. echo $str."n1 !!! $pat1 ($n): ".print_r($m1[0], true)
  38. ."n2 !!! $pat2 ($o): ".print_r($m2[0], true)
  39. ."n3 !!! $pat3 ($p): ".print_r($m3[0], true)
  40. ."n4 !!! $pat4 ($r): ".print_r($m4[0], true);
  41. // Note the difference between the three very helpful escape sequences in $pat2 (r), $pat3 (R) and in $pat4 (v) - for some applications at least.
  42.  
  43. /* The code above results in the following output:
  44. ABC ABC
  45.  
  46. 123 123
  47. def def
  48. nop nop
  49. 890 890
  50. QRS QRS
  51.  
  52. ~-_ ~-_
  53. 1 !!! /w$/mi (3): Array
  54. (
  55. [0] => C
  56. [1] => 0
  57. [2] => _
  58. )
  59.  
  60. 2 !!! /wr?$/mi (5): Array
  61. (
  62. [0] => C
  63. [1] => 3
  64. [2] => p
  65. [3] => 0
  66. [4] => _
  67. )
  68.  
  69. 3 !!! /wR?$/mi (5): Array
  70. (
  71. [0] => C
  72.  
  73. [1] => 3
  74. [2] => p
  75. [3] => 0
  76. [4] => _
  77. )
  78.  
  79. 4 !!! /wv?$/mi (5): Array
  80. (
  81. [0] => C
  82.  
  83. [1] => 3
  84. [2] => p
  85. [3] => 0
  86. [4] => _
  87. )
  88. */
  89. ?>
Add Comment
Please, Sign In to add comment