1.2. Accessing SubstringsYou want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form. 1.2.1. SolutionUse substr( ) to select your substrings: $substring = substr($string,$start,$length); $username = substr($_REQUEST['username'],0,8); 1.2.2. DiscussionIf $start and $length are positive, substr( ) returns $length characters in the string, starting at $start. The first character in the string is at position 0:
If you leave out $length, substr( ) returns the string from $start to the end of the original string:
If $start plus $length goes past the end of the string, substr( ) returns all of the string from $start forward:
If $start is negative, substr( ) counts back from the end of the string to determine where your substring starts:
If $length is negative, substr( ) counts back from the end of the string to determine where your substring ends:
1.2.3. See AlsoDocumentation on substr( ) at http://www.php.net/substr.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|