33 Votes

PHP: Remove arbitrary Characters at the Beginning and the End of a String

Tutorial by Stefan Trost | Last update on 2022-11-17 | Created on 2012-06-29

Sometimes we want to remove certain characters from a string from its front and/or its back. For example, this could be spaces, other white space, dashes or also other characters.

Exemplary, first let's have a look at the following three strings:

$a = "  Front and Rear Spaces should be removed.  ";
$b = "- front-and-rear-dashes-should-be-removed -";
$c = " - Dashes and Spaces should be reomoved. - ";

From the first string, we would like to remove the front and rear spaces, from the second we would like to remove the front and rear dashes and from the third string, we want to remove both.

The following PHP code is doing this exactly:

$a = trim($a);       // "Front and Rear Spaces should be removed."
$b = trim($b, '-');  // " front-and-rear-dashes-should-be-removed "
$c = trim($c, '- '); // "Dashes and Spaces should be reomoved."

We use the function trim(), to which we pass our string we want to cut as a first parameter. If we use trim() without any other parameter, each whitespace is removed from the back as well as from the front. In PHP, these are normal spaces, horizontal and vertical tabular signs (\t and \v), line breaks (\r and \n) as well as the zero-byte (\0). Our first example with the string $a demonstrates this.

If we want to remove other characters than the default whitespace characters, we can pass them to the trim() function as a second parameter. We do this in the second and the third example with the strings $b and $c. In the second example, we only hand over a hyphen as a second parameter. This means that no whitespace is removed and the spaces remain at the back and the front just like the hyphens within the string. In the third example, we add a space and remove both spaces and hyphens from the beginning and the end of the string.

Remove Ranges of Characters

The specification of individual characters, as we did in our third example above, has one disadvantage: If we want to remove a lot of characters, it becomes cumbersome. Fortunately, PHP offers an easy way to specify entire ranges of characters in order to remove them with the trim function:

$s = 'abcdefghij';
$s = trim($s, 'a..f');  // "ghij"

Here we would like to remove all the characters from a to f from the string "abcdefghij". With the specification a..f, we are spared to list all of those letters individually. The statement "a..f" leads to the same result as if we would write "abcdef". For both cases we receive the result "ghij".

Incidentally, the specification does not necessarily have to be done by the characters as such. We can also work with codepoints. Exposed with codepoints, the parameter for the letters a to f from our example would be "\x61..\x66".

How to add further Characters to the Default Whitespace

As we saw in our first examples, we lose our standard whitespace as soon as we use the second parameter. The moment when we passed the hyphen as a second parameter, the whitespace was no longer removed. So what can we do if we want to add some individual other characters to the standard function of trim()?

The answer is: We must additionally also hand over all default whitespace characters in the second parameter. This works with " \n\r\t\v\0", as the next example shows:

$a = trim($s);
$b = trim($s, " \n\r\t\v\0");

Here, both lines, once with and once without the second parameter, lead to the same result.

If we would like to add further characters, we can simply add them additionally:

$c = trim($s, " \n\r\t\v\0-");
$d = trim($s, " \n\r\t\v\00..9");

In the first line we only added the hyphen, in the second line we added the numbers 0 to 9, defined via a range like we got to know in the section before.

Remove Characters only from the Beginning or the End

Regardless of whether we want to remove the standard whitespace characters or we want to define our own characters - with the trim() function, the characters are always removed from the front as well as from the back. But also in the event that we only want to remove characters from the front or only from behind, PHP offers a solution: ltrim() und rtrim(). The next 3 lines should clarify the functionality compared to trim():

$s = " abc ";
$a = trim($s);   // "abc"
$b = ltrim($s);  // "abc "
$c = rtrim($s);  // " abc"

We have a string $s that starts with a space and ends with a space. With the normal trim() function, both of these spaces are removed. On the other hand, with ltrim(), we only remove the whitespace from the front (l = left), with rtrim() we only delete the spaces from the back (r = right). Otherwise, these two functions can be used like the original.


About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile


Related Topics


Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.