How to remove whitespace or any characters from beginning or end of a string in PHP? Md Jahidul Islam (oneTarek): How to remove whitespace or any characters from beginning or end of a string in PHP?

Monday, May 9, 2011

How to remove whitespace or any characters from beginning or end of a string in PHP?

Today I have faced a problem to remove extra forward slash ( / ) from a URL. I was working with some string manipulation operations. I need to parse some URL programmatically. At first I have to confirm that there is no extra forward slash end of the URL. If my url is like http://example.com/php/ . First I need to make this to http://example.com/php. I know the PHP function trim() but it  Strip white space and some other characters ( \n \t \r \0 \x0B and given character) from the beginning and end of a string. But I have to remove only  from end of the string not from beginning. At last I have found another related PHP function rtrim(). I have also learn the function ltrim(). Here I am describing these three trimming functions.

trim() : Strip whitespace (or other characters) from the beginning and end of a string
ltrim() :
Strip whitespace (or other characters) from the beginning of a string
rtrim() : Strip whitespace (or other characters) from the end of a string

Functions Prototypes:

string trim ( string $str [, string $charlist ] )
string ltrim ( string $str [, string $charlist ] )
string rtrim ( string $str [, string $charlist ] )

These three function has two same parameters and same return type. First parameter is required and second is optional.
These functions return a string with whitespace stripped from the end and beginning or only end or only beginning of the given string.
Without the second parameter these functions will remove following  characters:
  • " " (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.
You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters. 
Trims occurances of every word in an array from the beginning and end of a string + whitespace and optionally extra single characters as per normal trim().

What is the different between  tirm() ltrim() and rtrim():

trim() is remove chars from both  left and right side of a string. You can use it to clear the white spaces some special chars and your defined char from both side of user inputted text. But if you need to clear only one side , then you should use ltrim() for left side and rtrim() for right.
My problem was to clear "/" only from right side of URL . I used rtrim();

Example of my use:

<?php
$url="http://example.com/php/class1/";
$newurl=rtrim($url, "/");
echo $newurl;   
?>

Another Example:

<?php

$text 
"\t\tThese are a few words :) ...  ";

$binary "\x09Example string\x0A"; 
$hello  "Hello World"; 
var_dump($text$binary$hello);

print 
"\n";
$trimmed rtrim($text);

var_dump($trimmed);
$trimmed rtrim($text" \t."); 

var_dump($trimmed);
$trimmed rtrim($hello"Hdle");

var_dump($trimmed);
// trim the ASCII control characters at the end of $binary
// (from 0 to 31 inclusive)
$clean rtrim($binary"\x00..\x1F"); 

var_dump($clean);

?>


OutPut:

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"

string(30) "        These are a few words :) ..."
string(26) "        These are a few words :)"
string(9) "Hello Wor"
string(15) "    Example string"

While inserting data from web forms to databases, trimming the posted values to remove the spaces from the left and right sides can be a good idea to avoid the user faults. This code have to be located before the DB operations

This post is the answer of the following questions and queries

  • How can i remove white space from the beginning of a string?
  • How can I clean extra # $ % ^ * @ or any given character from the end of line
  • How can I confirm whether the last char of a URL is a "/"
  • How to clean the left side of a string?
  • How to clean the right side of a string?
  • How to trim a string?
  • What are the different between all PHP trimming function?
  • PHP trim functions
  • PHP strip functions
  • PHP string cleaner functions
  • Trim Left Trim and Right Trim
  •  The simplest way to strip a newline form a text file is ltrim()
  • To remove an unwanted character - example "." - if exist or not
  • PHP tutorial for string trimming.


No comments:

Post a Comment