Wednesday 24 March 2021

Php: ltrim, rtrim, trim : Trim the string


Function

Description

ltrim

Strip whitespace (or other characters) from the beginning of a string

rtrim

Strip whitespace (or other characters) from the end of a string

chop

Alias of rtrim()

trim

Strip whitespace (or other characters) from the beginning and end of a string

 

Signatures

ltrim ( string $string , string $characters = " \n\r\t\v\0" ) : string

rtrim ( string $string , string $characters = " \n\r\t\v\0" ) : string

trim ( string $string , string $characters = " \n\r\t\v\0" ) : string

 

characters argument specifies the characters that you want to strip, simply list all characters that you want to be stripped.

 

Find the below working application.

 

trim_demo.php

#!/usr/bin/php

<?php

   $str1 = '     Hello      ';

   $ltrim_str = ltrim($str1);
   $rtrim_str = rtrim($str1);
   $trim_str = trim($str1);

   echo "\$str1 : '$str1'\n";
   echo "\$ltrim_str : '$ltrim_str'\n";
   echo "\$rtrim_str : '$rtrim_str'\n";
   echo "\$trim_str : '$trim_str'\n";
   
?>

 

Output

$./trim_demo.php 

$str1 : '     Hello      '
$ltrim_str : 'Hello      '
$rtrim_str : '     Hello'
$trim_str : 'Hello'

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment