PHP String Cleanup Functions
PHP provides functions that help to clean up strings by removing unwanted whitespace or other characters from the beginning and/or end of the string:
ltrim. PHP also provides another type of cleanup function:
strip_tags, which removes HTML and PHP tags from strings.
Let's start with
trim and related functions. The
strip_tags function is described and demonstrated below.
Cleanup Beginning and End of Strings
trim function removes whitespace or other characters you specify from both the beginning and end of the string passed to it. The
rtrim function removes these unwanted characters from the end of the string (right) while
ltrim removes them from the beginning (left).
ltrim functions all accept two arguments: the string to clean up, and an optional list of characters to remove. If you don't specify a second argument, these functions will remove the following whitespace characters from the beginning and/or end of the string: spaces, tabs (
\t), newlines (
\n), carriage returns (
\r), vertical tabs (
\x0B), and NUL-byte characters (
First we demonstrate the
trim function with two example strings. In the first string, white space characters are entered literally, while the second uses escape sequences:
// whitespace entered via keyboard $str = " Dear Mr. Martin, "; var_dump( trim($str) ); // string(16) "Dear Mr. Martin," // whitespace using escape sequences $str2 = "\tDear Mr. Martin,\n"; var_dump( trim($str2) ); // string(16) "Dear Mr. Martin,"
As the output of
var_dump shows, the result of applying
trim is the same for both strings. All the whitespace characters are removed from the beginning and end of the string.
Next we demonstrate the
ltrim function, which just removes characters from the left, the beginning of the string. We apply
ltrim to the strings from the example above:
// apply ltrim to both example strings var_dump( ltrim($str) ); var_dump( ltrim($str2) ); // View Source display: /* string(17) "Dear Mr. Martin, " */
The result is the same for both strings: the whitespace has been removed from the left but remains intact on the right.
var_dump to get an accurate view of the results of our trim functions. It gives us the length of the string and encloses it in quotes. Note that when viewed in the browser, line breaks and extra spaces that exist in the string cannot be seen. To see an accurate display of the string, view browser page source (Ctrl-U), or display the output inside
<pre> tags. Find out more about using
var_dump to display data.
Specifying Characters to Remove
As mentioned above, you can pass a second argument to
trim and related functions to specify the characters to remove. In this example, we demonstrate using
rtrim to remove a trailing comma:
// second argument specifies characters to trim from first var_dump( rtrim('Hello,', ',') ); // string(5) "Hello"
In the example below, our second argument specifies that we would like to remove spaces, tabs, and commas from the beginning and end of the string passed to the
// trim spaces, tabs, and commas var_dump( trim("\tHello, ", " \t,") ); // string(5) "Hello"
The output of
var_dump shows that we have succeeded in removing the specified characters in both examples.
Remove Range of Characters
You can specify a range of characters in the second argument by using two dots between them. For example, to remove integers, specify a range using
0..9. In the following example, our string includes integers and spaces at the beginning and end of the string. We can use
trim to remove them as follows:
// trim integers and spaces from left and right of string var_dump( trim('12 Hello World 34', ' 0..9') ); // string(11) "Hello World"
Strip HTML and PHP Tags
strip_tags function removes all HTML and PHP tags from the string passed to it. HTML attributes and values are removed, leaving only tag content. The following example demonstrates:
// example string with HTML markup $str = <<<EOS <p class="txt">Some paragraph text here.</p> <script src="http://example.com/script.js"></script> EOS; // apply strip_tags to above string $stripped = strip_tags($str); var_dump($stripped); // browser display (View Source shows more whitespace): // string(34) " Some paragraph text here. "