Sunday, January 26, 2014

Simple Regular Expression PHP

<?php
$subject = "abcDef123aa456";
$pattern = '/[0-9]+/';
preg_match_all($pattern, $subject,$matches);
print_r($matches);
?>
you will get the exact pattern..
Result:
Array ( [0] => Array ( [0] => 123 [1] => 456 ) )

<?php
$subject = "abcDef123aa456";
$pattern = '/[0-9]*/';
preg_match_all($pattern, $subject,$matches);
print_r($matches);
?>
you will be able to find at which position the numeric starts...
Result:
Array ( [0] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => 123 [7] => [8] => [9] => 456 [10] => ) )