954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how do you strip tabs, extra spaces and new lines

from a string that's pulled from a mysql database? i know stripping new lines is

$d = str_replace("\r\n","",$d);


but what about tabs and extra spaces?

Geonith
Newbie Poster
4 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

//Newline and tab space to single space

$from_mysql = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $from_mysql);

// Multiple spaces to single space ( using regular expression)

$from_mysql = ereg_replace(" {2,}", ' ',$from_mysql);

// Replaces 2 or more spaces with a single space, {2,} indicates that you are looking for 2 or more than 2 spaces in a string.

ultra vires
Junior Poster in Training
51 posts since Feb 2006
Reputation Points: 10
Solved Threads: 5
 
trim($d);

This function returns a string with whitespace stripped from the beginning and end of str . Without the second parameter, trim() will strip these 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.

Sam

helraizer
Light Poster
48 posts since Apr 2008
Reputation Points: 11
Solved Threads: 3
 

I need my code to be written in single line, but

return trim("
<script language='JavaScript' type='text/javascript'>
function visbuttons_event(event)
{
	for(var i=0; i<event.data.id.length; i++) {
... and so on");

trim did not trimmed anything and

str_replace(array("\r\n", "\r", "\n", "\t"), ' ', '{same text}');

did.
why trim did not work in this situation?

Elmes
Newbie Poster
1 post since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You