You are here: Home Programming PHP How to use Substr () PHP Function

How to use Substr () PHP Function

 

 

 

How to use Substr () PHP Function in PHP

Definition: The Substr () PHP Function is used to return part of a string. It is written as substr (string, start, optional length);.
The string is whatever string you want to return a portion of.


The start is how many characters in to skip before starting. Setting this to 3 would skip the first three and start returning at the forth character. Setting this to a negative number will start counting backwards from the end of the string.


Length is an optional parameter. If you set this to a positive number, it will return that number of characters. If you set this to a negative number it will count that many numbers from the end of the string and return whatever is left in the middle.


Examples:

//this will return "welcome"
 echo substr('welcome', 1);

 // this will return "come"
 echo substr('welcome', 3);

 
 // this will return 'wel'
 echo substr('welcome', 0, 3);

 
 // this will return 'ome'
 echo substr('welcome', -3);

 
 // this will return 'elcom'
 echo substr('welcome', 1, -1);

 
 // this will return 'lc'
 echo substr('welcome', 2, -3);


Clientele

 

SIGN UP RIGHT NOW FOR EMAIL UPDATES

Share this post


blog comments powered by Disqus

Print PDF

HTML Codes
opinions powered by SendLove.to

Our valuable member Khabza has been with us since Thursday, 08 July 2010.

Show Other Articles Of This Author

Back to top
SA Topsites :: Simphiwe - Find me on Bloggers.com

Copyright © Green IT Web - Blog by Khabza in Cape Town 2012

Template by Joomla 1.7 Templates & Szablony Joomla.