Dec
02
2008
1

Strings and Patterns: Extracting

PHP has a function substr(); which allows extracting a substring from a larger string. It can have up to three parimiters:
substr($mainstring, $startpoint, $length);
The Mainstring is the string in which we want to get the contents out of, startpoint is the index number (remember first character is 0, second character is 1) and the length is the amount of characters we want to return leaving this blank will not set a limit on the length.

The starting index can be specified as either a positive
integer (meaning the index of a character in the string starting from the beginning)
or a negative integer (meaning the index of a character starting from the end). Here
are a few simple examples:

$x =1234567’;
echo substr ($x, 0, 3); // outputs 123
echo substr ($x, 1, 1); // outputs 2
echo substr ($x, -2); // outputs 67
echo substr ($x, 1); // outputs 234567
Written by Adam in: 07. Strings and Patterns | Tags: ,

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS