Parse String
Parses the string
into variables.
code
$str = "a=1&c[]=3&c[]=4";
parse_str($str, $output);
print_r($output);
Parse Url
Parse a URL and return
its components.
code
$parts = parse_url("http://doc.com/test.php?a=1&b=c");
print_r($parts);
Strtok
Splits a string (str) into smaller strings,
tokens.
Note that only the
first call to strtok uses the string argument.
code
$str = "java.lang.Object";
echo $tok_1 = strtok("java.lang.Object", ".");
echo $tok_2 = strtok(".");
Get the last part of a string
after a given character.
code
$str = "java.lang.Object";
$rev = strrev("java.lang.Object");
$lastToken = strtok($rev, ".");
$lastToken = strrev($lastToken);
echo $lastToken;
Get username from an
email address.
code
$username = strtok("john.wayne@movies.com", "@");
echo $username;
Strstr returns
what is after searched character.
code
$username = strstr("john.wayne@movies.com", "@");
echo $username;
Whitelist
Match a string against a
"whitelist" mask of allowed characters.
Strspn returns the
length of the initial segment of the string ...
that does contain any of the
characters from the mask.
code
echo strspn("133445xxx123yyy", "12345");
echo strspn("133 12345", "12345");
echo strspn("__12345__", "12345");
You can use this function with strlen to
check illegal characters.
This function is
significantly faster than the equivalent preg_match().
code
$allowed = "0123456789+. ";
$phone = "Phone: +021 072.88.544545";
$phone2 = "+021 072.88.544545 (phone)";
echo strspn($phone, $allowed);
echo strspn($phone2, $allowed);
$allowed = "0123456789+. ";
$phone = "+021 / 072.88.544545";
if (strlen($phone) != strspn($phone, $allowed)) {
echo "not allowed";
}