PHP and MySQL (54 Blogs) Become a Certified Professional

How To Best Utilize Trim Function In PHP?

Published on Sep 03,2019 64 Views
How To Best Utilize Trim Function In PHP?

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

PHP function is a portion of code that can be recycled many times. It can take effort as argument list and reappearance value. There are thousands of built-in functions in PHP. In this article we will be exploring Trim Function In PHP. Following pointers will be covered in this article,

So let us get started then,

Features Of PHP Functions

Code Reusability 

PHP functions are defined only once and can be raised many times, like in other programming languages. 

Less Code

It protects a lot of code because you don’t need to write the logic several times. By the use of function, you can write the logic only one time and use it again and again as per the need . 

Easy to understand

PHP functions discrete the programming logic. Therefore, it is calmer to understand the drift of the submission because every logic is divided in the form of functions. 

We can stipulate a default argument value in function. While calling PHP function if you don’t stipulate any argument, it will take the default argument. 

Moving on with this article on Trim Function In PHP

Trim Function In PHP

Removes characters from both sides of a string (“He” in “Head” and “d!” in “sherd”) 

<?php $str = "Hello sherd!";echo $str."<br>";echo trim($str,"Hed!");?>

The trim () function eliminates whitespace and other predefined characters from both sides of a string. 

Related Functions h3

ltrim()

Eliminates whitespace or other predefined characters from the left side of a string. rtrim() – Eliminates whitespace or other predefined characters from the right side of a string. 

Syntax 

trim(string,charlist)

Trim is the fastest way to remove first and last character

Sample Code

<?php $s = '/catalog/lyustry/svet/dom-i-svet/';
$times = 100000;
print cycle("str_preg('$s');", $times);
print cycle("str_preg2('$s');", $times);
print cycle("str_sub_replace('$s');", $times);
print cycle("str_trim('$s');", $times);
print cycle("str_clear('$s');", $times);
//print_r(str_preg2($s));
function cycle($function, $times){
$count = 0; if($times < 1){
return false; } $start = microtime(true); while($times > $count){
eval($function); $count++; } $end = microtime(true) - $start; return "n $function exec time: $end"; }
function str_clear($s){ $s = explode('/', $s); $s = array_filter($s, function ($s){if(!empty($s)) return true;}); return $s; } function str_preg2($s){
$s = preg_replace('/((?<!.)/(?=.))?((?<=.)/(?!.))?/i', '', $s); $s = explode('/', $s); return $s; } function str_preg($s){
$s = preg_replace('/^(/?)(.*?)(/?)$/i', '$2', $s); $s = explode('/', $s); return $s; } function str_sub_replace($s){
$s = str_replace('/' , '' , mb_substr( $s , 0, 1)) . mb_substr( $s , 1, -1) . str_replace('/', '', mb_substr( $s , -1));
$s = explode('/', $s); return $s; } function str_trim($s){ $s = trim($s, '/'); $s = explode('/', $s); return $s; }

Parameters

The function accepts one mandatory parameter and one optional parameter as shown in the above syntax and described below. 

$string  

This parameter signify the string from which the whitespace and predefined characters from left and right are to be removed. 

$charlist 

This is an optional parameter which specifies the characters that are to be removed from the string. If this is not mentioned then all of the following characters will be removed. 

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

Return Value 

It returns the modified string by removing the whitespace and also the predefined characters from both sides of a string that is left and right. 

Moving on with this article on Trim Function In PHP

PHP Ltrim Function 

The ltrim() function is a built-in function in PHP which removes whitespaces or other characters from the left side of a string. 

ltrim($string,$charlist)

Parameters 

The function ltrim() accepts two parameters as shown in the above syntax. Out of these two parameters, one is mandatory while the other one is optional. They are discussed in details below: 

$string

This mandatory parameter specifies the string to be checked. 

$charlist 

This optional parameter specifies which characters are to be removed from the string . In case, this parameter is not provided, the following characters are removed . 

“” – NULL “t” – tab “n” – new line “x0B” – vertical tab 

“r” – carriage return ” “ – ordinary white space 

Return Value 

Returns the modified string. 

Sample Program

This program shows the use of ltrim() function without any specified list of characters to be removed.

<?php
$string = "time for edureka";
echo "Subsidize ".ltrim($string);
?>

Output

Subsidize time for edureka 

Sample Program

This program shows the use of ltrim() function with a specified list of characters to be removed. 

<?php
$string = "!!! (( !!)) time for edureka"; echo ltrim($string, "! ()");
?>

Output

Time for edureka 

Moving on with this article on Trim Function In PHP

PHP Rtrim Function 

The rtrim() function is a built-in function in PHP which removes whitespaces or other characters (if specified) from the right side of a string. 

Syntax

rtrim( $string, $charlist )

Parameters 

The function rtrim() accepts two parameters as shown in the above syntax. Out of these two parameters, one is mandatory while the other one is optional. 

$string 

This mandatory parameter specifies the string to be checked. 

$charlist 

This optional parameter specifies which characters are to be removed from the string. In case, this parameter is not provided, the following characters are removed : 

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

Return Value

Returns the modified string. 

Sample Program

This program shows the use of rtrim() function without any specified list of characters to be removed.

<?php
$string = &ldquo; Time for "; echo rtrim($string)." edureka";
?>

Output 

Time for edureka 

Sample Program

This program shows the use of rtrim() function with a specified list of characters to be removed. 

<?php
$string = "Time for !!! (( !!))";
echo rtrim($string, "! ()")." Edureka ";
?>

Output

Time for Edureka 

Parameters 

string It is used to specifies string to check 

charlist It specifies which character to remove 

Remove whitespaces from both sides of a string: 

<?php$str="HelloWorld!";echo"Withouttrim:".$str;echo"<br>";echo"With trim:" . trim($str);?>

Output 

<!DOCTYPE html> <html> <body> 

Without trim: Hello World! <br>With trim: Hello World! </body> </html> 

Now with this, we have come to the end of the Trim Function In PHP article. I Hope you guys enjoyed this article and understood the concepts of PHP. So, with the end of this PHP Tutorial, you are no longer a newbie to the scripting language.

If you found this PHP Tutorial blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”Trim Function IN PHP” and I will get back to you.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.