w3resource

PHP: trim() function

Description

The trim() function is used to remove the white spaces and other predefined characters from the left and right sides of a string.

Version:

(PHP 4 and above)

Syntax:

trim(string_name, char_list)

Parameters:

Name Description Required /
Optional
Type
string_name The string to be trimmed. Required String
char_list Specifies the character(s) to remove. Without this remove the following " " an ordinary space.
"\0" - the NUL-byte.
"\t" - tab.
"\n" - line feed.
"\x0B" - a vertical tab.
"\r" - a carriage return.
Optional String

Return value:

The trimmed string.

Value Type: String.

Pictorial Presentation

string_trim

Example - 1:

<?php
$string_name=' Welcome to w3resource.com ';
$new_string=trim($string_name);
echo $new_string;
?>

Output:

Welcome to w3resource.com

View the example in the browser

Example - 2 :

<?php
$string_name="	Welcome to w3resource.com";
$new_string=trim($string_name,"\t");
//echo $new_string;
var_dump($new_string);
?>

Output :

string(25) "Welcome to w3resource.com"

View the example in the browser

See also

PHP Function Reference

Previous: substr
Next: ucfirst



PHP: Tips of the Day

Get the full URL in PHP

Have a look at $_SERVER['REQUEST_URI'], i.e.

Example:

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

Ref : https://bit.ly/2ZYItNJ