PHP ltrim: Strip Whitespaces from the Beginning of a String (original) (raw)
Summary: in this tutorial, you’ll learn how to use the PHP ltrim()
function to remove whitespace characters or other characters from the beginning of a string.
Introduction to the PHP ltrim() function #
The PHP ltrim()
function removes the whitespace characters or other characters from the beginning of a string.
Here’s the syntax of the ltrim()
function:
ltrim ( 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" ) : string
Code language: PHP (php)
The ltrim()
has the following parameters:
$string
is the input string$characters
is one or more characters that you want to remove from the beginning of the string.
By default, the ltrim()
function strips the characters in the list " \n\r\t\v\0"
from the beginning of the string.
If you want to strip other characters, you can specify them in the $characters
argument.
To remove a set of characters, you can use the ..
syntax. For example, to remove all characters from a to z, you pass the the following string to the $characters argument:
'a..z'
Code language: PHP (php)
It’s important to notice that the ltrim()
function doesn’t modify the input string ($string
). Instead, it returns a new string with the characters specified in the $characters
removed.
PHP ltrim() function examples #
Let’s take several examples of using the ltrim()
function.
1) Using the PHP ltrim() function to remove whitespace characters #
This example uses the ltrim()
function to strip all the space characters from the beginning of a string:
`<?php
$title = ' PHP tutorial'; cleantitle=ltrim(clean_title = ltrim(cleantitle=ltrim(title);
var_dump($clean_title);`Code language: PHP (php)
Output:
string(12) "PHP tutorial"
Code language: PHP (php)
2) Using the PHP ltrim() function to remove other characters #
The following example uses the ltrim() function to remove the slash (/
) at the beginning of a string:
`<?php $uri = '/php-ltrim'; newuri=ltrim(new_uri = ltrim(newuri=ltrim(uri, '/');
echo $new_uri;`Code language: PHP (php)
Output:
php-ltrim
Code language: PHP (php)
Summary #
- Use the PHP
ltrim()
function to remove the whitespace characters or other characters from the beginning of a string.
Did you find this tutorial useful?