Parse String
Parses the string into variables.
//$str = $_SERVER['QUERY_STRING'];
$str = "a=1&c[]=3&c[]=4";
parse_str($str, $output);
print_r($output); // [ a=> 1, c=>[3, 4] ]
Parse Url
Parse a URL and return its components.
$parts = parse_url("http://doc.com/test.php?a=1&b=c");
print_r($parts);
/*
Array (
[scheme] => http
[host] => doc.com
[path] => /test.php
[query] => a=1&b=c
)
*/
Strtok
Splits a string (str) into smaller strings, tokens. Note that only the first call to strtok uses the string argument.
$str = "java.lang.Object";
echo $tok_1 = strtok("java.lang.Object", "."); // java
echo $tok_2 = strtok("."); // lang
Get the last part of a string after a given character.
$str = "java.lang.Object";
$rev = strrev("java.lang.Object"); // tcejbO.gnal.avaj
$lastToken = strtok($rev, "."); // tcejbO
$lastToken = strrev($lastToken);
echo $lastToken; // Object
Get username from an email address.
$username = strtok("john.wayne@movies.com", "@");
echo $username; // john.wayne
Strstr returns what is after searched character.
$username = strstr("john.wayne@movies.com", "@");
echo $username; // @movies.com
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.
echo strspn("133445xxx123yyy", "12345"); // 6
echo strspn("133 12345", "12345"); // 3
echo strspn("__12345__", "12345"); // 0
You can use this function with strlen to check illegal characters.
This function is significantly faster than the equivalent preg_match().
$allowed = "0123456789+. ";
$phone = "Phone: +021 072.88.544545";
$phone2 = "+021 072.88.544545 (phone)";
echo strspn($phone, $allowed); // 0
echo strspn($phone2, $allowed); // 19
$allowed = "0123456789+. ";
$phone = "+021 / 072.88.544545";
if (strlen($phone) != strspn($phone, $allowed)) {
echo "not allowed"; // because of "/" char
}
Last update: 382 days ago