Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts

Friday 27 July 2012

PHP Regular Expressions



<?php
$text = "Hello World";
if ( preg_match( "/r.*?d/", $text, $array ) )
 {
  print "<pre>\n";
  print_r( $array );
  print "</pre>\n";
 }
?>
 

Output :

Array
(
[0] => rld
)

Get an IP address

<?php
$test = "126.122.95.52";
if ( preg_match( "/(\d+)\.(\d+)\.(\d+)\.(\d+)/", $test, $arr ) )
 {
  print "<pre>\n";
  print_r( $arr );
  print "</pre>\n";
 }
?>
 

Output :

Array
(
[0] => 126.122.95.52
[1] => 126
[2] => 122
[3] => 95
[4] => 52
)