preg_grep() Regular Expression PHP Example

regular expression preg_grep function example - returns the array that match the given pattern

regular-expression-preg-grep-function-in-php.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>regular expression preg_grep function example - returns the array that match the given pattern</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Md Iqbal Hosan">
  <meta name="title" content="regular expression preg_grep function example - returns the array that match the given pattern">
  <meta name="keywords" content="php, example, code, preg_grep, function, validates email address, regular expression">
  <meta name="description" content="regular expression preg_grep function example - returns the array that match the given pattern">
  <style>
   h2, h3{background:#ccc; color:#000088;}
   h2{ padding:3px; margin:3px; font-size:22px;}
   h3{ padding:2px; margin:2px; font-size:17px;}
   p{ padding:3px; margin:3px;}
   body{ background:#FFFFFa;}
  </style>
 </head>
 <body>
  <h2>regular expression preg_grep function example - returns the array that match the given pattern</h2>
  <pre>
   <?php
    /*
     ---------------------------------------------------------------------------
     let us make an arry and fill it up with some integer and floating values
     ---------------------------------------------------------------------------
    */
    $myArray = array(10, 20, 30, 11.11, 12.12, 13.13);
    echo "<h3>Output of myArray</h4>";
    print_r($myArray);
    /*
     ------------------------------------------------------------------------------
     we want to get an array of floating point values by using preg_grep function
     ------------------------------------------------------------------------------
    */
    $floatingArray = preg_grep("/^(\d+)?\.\d+$/", $myArray);
    echo "<h3>floatingArray by using preg_grep regular expression function</h3>";
    print_r($floatingArray);
    /*
     --------------------------------------------------------------------------------------
     where 
     ^ start of the pattern
     $ end of the pattern
     \d decimal number
     + continue (>=1 quantifier)
     in words : decimal . decimal = floaging point values
     --------------------------------------------------------------------------------------
     
    */
    /*
     ------------------------------------------------------------------------------
     now we want to get an array of decimal values by using preg_grep function
     ------------------------------------------------------------------------------
    */
    $decimalArray = preg_grep("/^\d+$/", $myArray);
    echo "<h3>decimalArray by using preg_grep regular expression function</h3>";
    print_r($decimalArray);
   ?>
  </pre>
 </body>
</html>

regular expression preg_grep function example - returns the array that match the given pattern - output in the browser

 

Related Tutorial Examples

  1. Check Apache Web Server Version
  2. Check Loaded Module in Apache Web Server
  3. Get Loaded Extensions in Apache Web Server
  4. curl_version() - cURL or Client URL Version Information
  5. Enable or Disable WSDL Cache :: soap.wsdl_cache_enabled
  6. Google Maps API Tutorial Geocoder Class Example
  7. Display Yahoo Weather RSS Feed PHP Class Example
  8. 19 Examples to Learn MySQL
  9. basename() Get Filename PHP Filesystem Function Example
  10. 20 steps to make CakePHP Blog Project

No comments:

Post a Comment

leave your comments here..