PHP » Strings » substr()

Syntax:
string substr(string s, int start [, length])
s
The string.
start
Start position.
length
Number of characters to copy.

Returns part of a string.

substr() returns part of a string, starting at a specified position. A negative start position will give an offset from the end of the string. If the optional length is specified and positive, that is the maximum number of characters that will be copied. A negative length skips that many characters at the end of the string.

Examples

Code:
<?php

$s = "Fruit: Banana";
$item = substr($s, strpos($s, ":") + 1);
$category = substr($s, 0, strpos($s, ":"));
print "Category: $category, Item: $item";

?>
Output:
Category: Fruit, Item: Banana
Explanation:

A string is parsed using substr() and strpos() to find a category and an item based on the position of the colon.

See Also: