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 . First I need to make this to 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:

$newurl=rtrim($url, "/");
echo $newurl;   

Another Example:


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

$binary "\x09Example string\x0A"; 
$hello  "Hello World"; 

$trimmed rtrim($text);

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

$trimmed rtrim($hello"Hdle");

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




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.

New Design of Google Search Result Page is Coming Soon

Google has decided to change their search result page design. They already has made new design and it is steel testing. New design is running but it is being shown only for some special region and special person.

What is new in new design:
  1. Result title font size has been increased
  2. Distance between two result is higher than old version
  3. Snippets (descriptions) are shown in one line, maximum two lines.
  4. Real time contents get priorities, for that reason twitter is being come top
  5. Overall look is fresher than previous

Some Screen shorts  of New Search Result Page: