Jumat, 03 Januari 2014

PHP substr(), strpos(), strlen(), strrev()

Now we going to discuss about the use of 'substr', 'strpos', 'strlen', and 'strrev' in PHP

substr - return part of a string

   
   <php
      $string = "I Love PHP";
      $result = substr($string, 0, 1);    // returns "I"
      $result = substr($string, 2, 4);    // returns "LOVE"  
      $result = substr($string, -3);      // returns "PHP"
   ?>

strpos - find the numeric offset position

   
   <php
      $string = "I Love PHP";
      $result = strpos($string, "I");    // returns "0"
      $result = strpos($string, "L");    // returns "2"  
      $result = strpos($string, "P");    // returns "7"
      /*to know the next position of the same string you must give an offset higher than previous offset position*/
      $result = strpos($string, "P", 8); // returns "9"
   ?>

strlen - count the number of character in string (space included)

   
   <php
      $string = "I Love PHP";
      $result = strlen($string);         // returns "10"
   ?>

strrev - read string from end to start

   
   <php
      $string = "I Love PHP";
      $result = strrev($string);         // returns "PHP evoL I"
   ?>

Tidak ada komentar:

Posting Komentar