Categories: PHP

ltrim – PHP String Functions

Syntax :

ltrim ( string, charlist );

Description :

ltrim() function will truncate spaces or other characters from starting of input string. We can remove “hi” from the starting of input string “hi from tutorialmines.” by this function.


Parameter :

  • string – This is a Required parameter. This is a string on which function will be performed.
  • charlist – This is an Optional parameter. If you wont use the second parameter, ltrim() will strip below characters:

The following characters are removed if the charlist parameter is empty:

  • “\0” – NULL
  • “\t” – tab
  • “\n” – new line
  • “\r” – carriage return
  • ” ” –  ordinary white space
  • “\x0B” – vertical tab

Output :

This will return a trimmed string.


Related articles : trim , rtrim.


ltrim() – PHP Functions Example 1 :
<?php
// below statement will trim white spaces from left side of string."
$strExample = " Hi from tutorialmines!"; 
echo ltrim($strExample);
?>

In above example ,We have a string “Hi from tutorialmines! “. Now this function will trim blank spaces from left side of string. Output of above code in the browser is as below:

Hi from tutorialmines!

ltrim() – PHP Functions Example 2 :
<?php
// below statement will trim white spaces from left side of string."
$strExample = " \nHi from \ntutorialmines.\n\n";
echo ltrim($strExample);
?>

In above example ,We have a string ‘ \nHi from \ntutorialmines.\n\n ‘. Now this function will trim blank spaces from left side of string. Output in ViewSource of browser is below ( HTML output of the code above):

<html><head></head><body>
Hi from 
tutorialmines.

 
</body></html>

See below is the output of above code.

Hi from tutorialmines.

ltrim() – PHP Functions Example 3 : Using charlist parameter.
<?php
$strExample = "Hi from tutorialmines. This is a example of ltrim() function. Testing parameter."; 
echo ltrim($strExample,"Hi");
?>

In above example ,We have a string ‘Hi from tutorialmines. This is a example of trim() function. Testing parameter.’. Now this function remove “Hi” from the starting of string. See below is the output of above code.

from tutorialmines. This is a example of ltrim() function. Testing parameter.


ltrim() – PHP Functions Example 4 : Using charlist parameter with (..) specifying range of characters.
<?php
$strExample = "3579Hi from tutorialmines. This is a example of ltrim() function. Testing parameter."; 
echo ltrim($strExample,"A..Za..z0..9");
?>

In above example ,We have a string ‘3579Hi from tutorialmines. This is a example of ltrim() function. Testing parameter.’. Now this function remove “3579Hi” character from the starting of string by using range of characters with .. operator.

  • 0..9 – it will remove numbers from 0-9 from left side.
  • a..z – it will remove LOWERCASE alphabets from a to z  from left side.
  • A..Z – it will remove UPPERCASE alphabets from A to Z  from left side.

See below is the output of above code.

from tutorialmines. This is a example of ltrim() function. Testing parameter.


jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

11 months ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago