Sunday, February 6, 2011

Substring in PHP

To find a substring in PHP you will need to use substr() function provided by PHP.

Method Signature

string substr ( string $string , int $start [, int $length ] )

Parameter Description

string => Required. Specifies the string to return a part of.
start => Required. Specifies where to start in the string.
  • A positive number - Start at a specified position in the string.
  • A negative number - Start at a specified position from the end of the string.
  • 0 - Start at the first character in string.
length => Optional. Specifies the length of the returned string. Default is to the end of the string.
  • A positive number - The length to be returned from the start parameter.
  • Negative number - The length to be returned from the end of the string

Examples

echo substr("Hello world!",6); // world!
echo substr("Hello world!",6,5); // world
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems