Jump to: navigation, search

Php trim

From w3cyberlearnings

Contents

PHP function trim

This function strips white-space from the beginning and end of a string.

Syntax trim

  • string: string input to strip whitespace.

charlist


  • " " (ASCII 32 (0x20)), an ordinary space.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13 (0x0D)), a carriage return.
  • "\0" (ASCII 0 (0x00)), the NUL-byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.
trim(string, charlist);

Example 1

<?php

$str = "\t\tgood old day";
echo $str;
echo '<br/>';
echo trim($str);
?>

Output

good old day
good old day

Output:HTML view source

space in view source
<--------------------->good old day<br/>good old day

Example 2

<?php

$str ="\n\nGone now";
echo $str;
echo '<br/>';
echo trim($str);
?>

Output

Gone now
Gone now

Output:HTML view source

|
|space in view source
|
Gone now<br/>Gone now

Example 3

<?php
echo trim('Hello World','Hello');
?>

Output

World

Example 4

<?php

echo trim('You are done now, boss','Yso');//u are done now, b
echo trim('You are done now, boss','Y');  //ou are done now, boss
echo trim('You are done now, boss','Yo');  //u are done now, boss

?>

Example 5

<?php

$name = array('bob maat ', ' janny ', ' paul david ', ' jeson');

$all_nam = array_map("trim", $name);
var_dump($name);

var_dump($all_nam);
?>

Output

array(4) { 
[0]=> string(9) "bob maat " 
[1]=> string(7) " janny " 
[2]=> string(12) " paul david " 
[3]=> string(6) " jeson" } 

array(4) { 
[0]=> string(8) "bob maat" 
[1]=> string(5) "janny" 
[2]=> string(10) "paul david" 
[3]=> string(5) "jeson" }

Example 6

<?php

$binary = "\x09Example string\x0A";
echo $binary;
echo trim($binary);
?>

Output

Example string Example string

Output:HTML View Source

whitespace
<---------->Example string
Example string

Related Functions


Navigation
Web
SQL
MISC
References