Extracting Substrings


 snbstr() 


The substr() function returns a part of a string. You provide a string and the position of the first character to be extracted (keep in mind that the first character has the index 0). From this character on, the rest of the string is returned. If you only want to return a part of it, provide the length in the third parameter. The preceding code shows substr() in action and extracts Prep from PHP: Hypertext Preprocessor.

Extracting a Substring Using substr() (substr.php; excerpt)
 <?php   $php = "PHP: Hypertext Preprocessor";   echo substr($php, 15, 4); //returns "Prep" ?> 

TIP

If you want to count from the end of the string, use a negative value as the second parameter of substr():

echo substr($php, -12, 4);

If you provide a negative value for the third parameter of substr(), for example n, the last n characters are not part of the substring.

echo substr($php, -12, -8);

All of these calls to substr() return Prep and are included into the complete code.





PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net