The
substr function
The substr function extracts a substring out
of a string and returns it. The function receives 3 arguments:
a string value, a position on the string (starting to count from 0)
and a length.
Example1:
$a =
"university";
$k
= substr ($a, 3, 5); #get a substring from position 3
(starting from 0) and a lenght of 5 for string $a
$k is now
"versi"
$a =
"university";
$k
= substr ($a, -3, 5); #get a substring from position at
right at 3 and a lenght of 5
to the right for string $a
$k is now
"ity"
Noticed it only returns 3 letters not 5 because there are only 3
letters when counting from the
right to position 3.
$a
remains unchanged.
If
length is omitted, everything to the end of the string is returned.
Example
(Get information fromm Swiss-Port data entry of Aplysia)
The input file used for this example: swp_apl.txt