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?

Recommended Answers

All 4 Replies

//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.

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

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?

trim removes whitespace from the BEGINNING and END of the string, not between words. Eg: trim(" hello world ") will become "hello world", not "helloworld".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.