PHP String Functions & Regular
Expressions
String Functions
|
Function |
What it does |
Example |
|
Concatenation |
Add’s strings together. |
$x . $t |
|
Addslashes($str) |
Returns a string with backslashes before characters that |
string addslashes |
|
Rtrim($x) |
rtrim — Strip |
string rtrim |
|
Ltrim($x) |
Strip whitespace (or other characters) from the |
string ltrim |
|
Strlen($x) |
Returns the length of the given string . |
int strlen |
|
Strpos($str,$x) |
Find position of first occurrence of |
int strpos |
|
Explode($d,$str) |
explode — Split a string by string |
array explode |
|
implode($d,$str) |
Join array elements with a string. |
string implode |
|
Soundex($str) |
Calculate the soundex key of a |
string soundex |
|
Metaphone($str) |
Calculate the metaphone key of a |
string metaphone |
Regular Functions
|
Function |
Description |
Example |
|
Ereg($p,$str,[$m]) |
Regular expression match |
int ereg int eregi |
|
Eregi($p,$str, [$m]) |
Case insensitive regular expression |
int eregi
|
|
Ereg_replace($p,$r,$str) |
Replace regular expression |
string ereg_replace |
|
Sql_regcase($p) |
Make regular expression for case |
string sql_regcase |
|
|
Split string into array by regular |
array split |
|
Preg_match() |
Perform a regular expression match |
int preg_match |
|
Preg_match_all() |
Perform a global regular expression |
int preg_match_all |
|
Preg_replace() |
Perform a regular expression search |
mixed |
|
Preg_split() |
Split string by a regular expression |
array preg_split |
MetaCharacters
|
. |
Any ONE Character |
|
[A-Z0-9] |
Any one of the chars |
|
[^a-cx-z] |
Not one of the chars |
|
^ |
Begins with |
|
$ |
End with |
|
X{2} |
2 consecutive “x” |
|
X{2,5} |
Between 2 and 5 consecutive “x” |
|
X? |
0 or 1 “x” |
|
X+ |
1 or more “x” |
|
d |
Digit |
|
D |
Non-digit |
|
w |
Word |
|
W |
Non-word |
|
s |
Space char |
|
S |
Not a space char |
|
(x|z) |
X or Z |