PHP trim: Remove whitespaces or other Characters from Both ends of a String (original) (raw)

Skip to content

Summary: in this tutorial, you’ll learn how to use the PHP trim() function to remove whitespace or other characters from both ends of a string.

Introduction to the PHP trim() function #

The trim() function removes the whitespaces or other characters from the beginning and end of a string.

Here’s the syntax of the trim() function:

trim ( string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">string , string </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>characters = " \n\r\t\v\0" ) : stringCode language: PHP (php)

The trim() function has two parameters:

By default, the trim() function removes the following characters:

Character ASCII Hex Description
” “ 32 0x20 a space
“\t” 9 0x09 a tab
“\n” 10 0x0A a new line
“\r” 13 0x0D a return
“\0” 0 0x00 a NUL-byte
“\v” 11 0x0B a vertical tab

If you want to remove other characters, you can specify them in the $characters argument.

To specify a range of characters to remove, you can use the ‘..’. For example, to remove all ASCII control characters from both ends of a string, you use the following value for the $characters argument:

'\x00..\x1F'Code language: PHP (php)

It’s important to note that the trim() function doesn’t change the input string. It returns a new string with all the $characters removed.

PHP trim() function examples #

Let’s take some examples of using the PHP trim() function.

1) Using the PHP trim() function to remove whitespace from both ends of a string #

The following example uses the trim() function to remove spaces from the beginning and the end of a string:

`<?php

$str = ' PHP '; newstr=trim(new_str = trim(newstr=trim(str);

var_dump($new_str);`Code language: PHP (php)

Try it

Output:

string(3) "PHP"Code language: plaintext (plaintext)

2) Using the PHP trim() function to remove other characters #

Suppose you have the following URL:

https://www.phptutorial.net/api/v1/posts/Code language: PHP (php)

To get the request URI, you access the $_SERVER array with the key 'REQUEST_URI':

`<?php uri=uri = uri=_SERVER['REQUEST_URI'];

echo $uri;`Code language: PHP (php)

Output:

/api/v1/posts/Code language: plaintext (plaintext)

The URI contains both leading and trailing slashes.

To remove the slashes (/) from both ends of the URI, you can use the trim() function:

`<?php uri=uri = uri=_SERVER['REQUEST_URI'];

echo trim($uri,'/');`Code language: PHP (php)

Output:

api/v1/postsCode language: plaintext (plaintext)

Summary #

Did you find this tutorial useful?