String Basics

Comparing Strings

Formatting Strings


$format = 'The %2$s contains %1$04d monkeys'; // 2$=>the second argument, 04d=>with 4 digits
echo printf($format, $num, $location); // The tree contains 0005 monkeys
$s = 'monkey'; $t = 'many monkeys';
printf("[%s]\n",$s); // standard string output [monkey]
printf("[%10s]\n",$s); // right-justification with spaces [    monkey]
printf("[%-10s]\n",$s); // left-justification with spaces [monkey    ]
printf("[%010s]\n",$s); // zero-padding works on strings too [0000monkey]
printf("[%'#10s]\n",$s); // use the custom padding character '#' [####monkey]
printf("[%10.10s]\n",$t); // left-justification but with a cutoff of 10 characters [many monke]

$number = 1234.56;
setlocale(LC_MONETARY, 'en_US');
$money_format = money_format('%i', $number) . "\n"; // USD 1,234.56
$english_format_number = number_format($number, 2, '.', ','); // 1,234.56

$test = "string 1234 string 5678";
$result = sscanf($test, "%s %d %s %d"); // $result = array('string','1234','string','5678');

Printf Formatting Specifiers

Escape Sequences (Control Characters)


substr($string, $start, $length)
– returns the substring from the start position with the given length
– a negative start to count from the end
– a negative length to count from the end
– return FALSE on failure or an empty string

substr_compare($string1, $string2, $offset, $length, $case-insentivity)
– binary safe comparison of two strings from an offset, up to length characters
– return 0 if they are equal

substr_count($haystack, $needle, $offset)
– count the number of substring occurrences

substr_replace($string, $replacement, $start, $length)
– replace text within a portion of a string

strstr($haystack, $needle, TRUE | FALSE)  // TRUE is before, FALSE is including the $needle and after
stristr($haystack, $needle, TRUE | FALSE)  // case-insensitive
strchr($haystack, $needle, TRUE | FALSE )
– return the part of $haystack after(and including) or before the first occurrence of $needle
– e.g. $email  = ‘name@example.com’; $domain = strstr($email, ‘@’); // $domain==’@example.com’
$user = strstr($email, ‘@’, TRUE); // $user == ‘name’

strpos($haystack, $needle, $offset)
stripos($haystack, $needle, $offset)   // case-insensitive
strrpos($haystack, $needle, $offset)   // search from end
– find the position of the first occurrence of a substring in a string (optionally ignore the first few elements as specified by $offset)

strpbrk($string, $charlist)   // case sensitive
– break the string starting from the character found and return, or FALSE if it is not found

str_replace($search, $replace, $subject)
str_ireplace($search, $replace, $subject)  // case-insensitive
– find and replace, $search can be an array, an optional $count for the 4th argument returns the number of replacements

strtr($string, $from, $to)
strtr($string, $replace_array)
– translate characters or replace substrings
– e.g. echo strtr(“baab”, “ab”, “01”); // 1001
– e.g. $trans = array(“ab” => “01”); echo strtr(“baab”, $trans); // ba01

str_repeat($string, $times)
– repeat a string a number of $times

– split the string into an array, each with a length equals or below the maximum length

– returns the length of the initial segment of subject that contains only characters from mask

– returns the length of the initial segment of subject that does not contain any characters from mask

– reverse a given string

– shuffle the string

– parse the string into variables, e.g. $str = “first=value&arr[]=foo+bar&arr[]=baz”; parse_str($str); // $first == ‘value’

– parse the url to get the information in an associative array

– PHP > 5.3, parse a CSV string into an array

– return 1 if magic_quotes_gpc is on, 0 is off

– quote string with backslashes before “,’,\,NUL
– unquote a quoted string
addcslashes($string, $charList_to_add)
– quote string with backslashes before characters listed in $charList_to_add, [\]^_~ will always be quoted
stripcslashes($string, $charList_to_add)
– unquote a string quoted with addcslashes

– split and return the $string into a smaller chunk based on the $delimiter (not included)
– subsequent calls only requires strtok($delimiter), delimiter not included

– split the string into smaller chunks, e.g. used in base64_encoding() or used in email output

– one-way encryption of $string, return a hashed string using the standard Unix DES-based algorithm or alternative algorithms that may be available on the system (from PHP 5.3, PHP contains its now implementation of algorithms), e.g. $hashed_password = crypt(‘mypassword’); if (crypt($user_input, $hashed_password) == $hashed_password) { echo “Password verified!”; }
– slower for better protection
– crypt_md5() vs md5() // crypt_md5() encrypts many times using md5 algorithm.

– returns the hash according to the $algo selected
md5($string) // not for password
– returns the hash as a 32-character hexadecimal number
crc32($string) // not for password
– generates the cyclic redundancy checksum polynomial of 32-bit lengths, usually to validate the integrity of data being transmitted
sha1($string) // not for password
– Returns the hash as a 40-character hexadecimal number

– shifts every letter by 13 places in the alphabet, encode and decode with the same function

mbstring(Multibyte String)

PCRE(Perl Compatible Regular Expressions)





Unicode Character Properties (for UTF-8)

Pattern Modifiers

$pattern = ‘/^\s+/i’;


preg_match ($pattern, $subject, $matches, $flags, $offset);   // perform a regular expression match, stop once matched, return 1 if matched, 0 if not matched, FALSE if error occurs
preg_match_all ();  // Perform a global regular expression match, returns the number of matches
preg_grep ($pattern, $array);  // returns the array consisting of the elements of the input array that match the given pattern, keys preserved, like preg_filter except without replacement
preg_filter ($pattern, $replace, $subject);   // returns and replace the $subject when there is a match, $subject can be arrays
preg_replace ($pattern, $replace, $subject);   // returns all the $subject after replacement with matches
preg_replace_callback ($pattern, $callback, $subject)  // transform using a callback function
$array = preg_split ($pattern, $string);  // the array contains the $string split with $pattern
preg_quote ($sting, $optional_delimiter); // format the string into a PECL pattern with escape characters
preg_last_error ();   // return the error code of the last regex execution, e.g. PREG_NO_ERROR, PREG_BAD_UTF8_OFFSET_ERROR