String Functions in PHP
From Free Knowledge Base- The DUCK Project: information for everyone
. ------------------------------------------------------------------------------- . . PHP uses the '.' dot operator to join strings . . $first_name = 'Charlie'; . $last_name = 'Brown'; . . $full_name = $first_name . ' ' . $last_name; . $full_name = "$first_name $last_name"; . $full_name = "{$first_name} {$last_name}"; . . ------------------------------------------------------------------------------- . . LEFT MID RIGHT Equivalent . . Depending on your background, you may have come across these string functions: . LEFT, MID, RIGHT. Below I will show you how you can achieve RIGHT in PHP using . substr. . . Syntax for RIGHT is RIGHT(string, length) - eg if string = "abcdefgh" and you . issue RIGHT(string, 4), you get "efgh", the last four characters. You can . achieve a similar result in PHP by using substr(string, -4). . . $rest = substr("abcdef", 1); // returns "bcdef" . $rest = substr("abcdef", 1, 3); // returns "bcd" . $rest = substr("abcdef", 0, 4); // returns "abcd" . $rest = substr("abcdef", 0, 8); // returns "abcdef" . . // Accessing via curly braces is another option . $string = 'abcdef'; . echo $string{0}; // returns a . echo $string{3}; // returns d . . $rest = substr("abcdef", -1); // returns "f" . $rest = substr("abcdef", -2); // returns "ef" . $rest = substr("abcdef", -3, 1); // returns "d" . . ------------------------------------------------------------------------------- . . preg_grep . preg_last_error . preg_match_all . preg_match . preg_quote . preg_replace_callback . preg_replace . preg_split . . ------------------------------------------------------------------------------- . . preg_replace -- Perform a regular expression search and replace . . echo preg_replace($pattern, $replacement, $string); . . $searchstr = preg_replace('#[^a-zA-Z0-9_-]#', ' ', $searchstr); . . .