Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $subject = 'abc#
- 123#
- ';
- $pattern = '/1...$/';
- preg_match_all($pattern,$subject,$matches); // no match
- $pattern = '/1...(.)$/';
- echo bin2hex($matches[1]); // 28
- $subject = 'abc#
- 123#
- ';
- $pattern = '/1...(.)$/';
- preg_match_all($pattern,$subject,$matches);
- echo bin2hex($matches[1]); // 28
- // 28 is equivalent of r or CR(carriage return)
- /1...$/m
- <?php
- // Various OS-es have various end line (a.k.a line break) chars:
- // - Windows uses CR+LF (rn);
- // - 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(?).
- $str="ABC ABCnn123 123rndef defrnop noprn890 890nQRS QRSrr~-_ ~-_";
- // C 3 p 0 _
- $pat1='/w$/mi';
- $pat2='/wr?$/mi';
- $pat3='/wR?$/mi';
- $pat4='/wv?$/mi';
- $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);
- 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);
- // Note the difference between the three very helpful escape sequences in $pat2 (r), $pat3 (R) and in $pat4 (v) - 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 !!! /wr?$/mi (5): Array
- (
- [0] => C
- [1] => 3
- [2] => p
- [3] => 0
- [4] => _
- )
- 3 !!! /wR?$/mi (5): Array
- (
- [0] => C
- [1] => 3
- [2] => p
- [3] => 0
- [4] => _
- )
- 4 !!! /wv?$/mi (5): Array
- (
- [0] => C
- [1] => 3
- [2] => p
- [3] => 0
- [4] => _
- )
- */
- ?>
Add Comment
Please, Sign In to add comment