tags:

views:

62

answers:

5

Hi Guys,

I am writing html content to a BML file, how can I remove new lines/whitespace so it is all in one long string?

does preg_replace("\n","") work?

+1  A: 

If you just want to remove newline characters, str_replace is all you need:

$str = str_replace("\n", '', $str);
VoteyDisciple
you forgot the whitespaces :)
Hannes
+2  A: 

preg_match only compares and returns matches, it does not replace anything, you could use $string = str_replace(array(" ","\n"),"",$string)

Hannes
A: 

The preg_match method does not work in this case as it does not replace characters but tries to find matches.

Marius Schulz
+3  A: 

Better use platform independent ending line constant PHP_EOL which is equivalent to array("\n", "\r", "\r\n") in this case...

$html = str_replace(PHP_EOL, null, $html);
Otar
This isn't working for me, any ideas why?
Liam Bailey
doesn't consider whitespaces, but good idea to use PHP_EOL
Hannes
@Liam hard to say...
Otar
@Hannes For removing whitespace there is a `trim()` function in PHP.
Otar
@Otar trim only strips whitespace (or other characters) from the beginning and end of a string ... otherwise str_replace wouldn't have been necessary in the first place :)
Hannes
@Hannes What stand in front of using them both? :P
Otar
Well, he wants to remove all white spaces, even between chars, so after removing all linebreaks, and remaining white spaces at the begining and end of the string, the white spaces between, would still remain :P
Hannes
A: 

I would replace all \n with a space, then replace double spaces with a single space (being as HTML does not use more than one space by default)

$str = str_replace('\n', ' ', $str);
$str = str_replace('  ', ' ', $str);
Thomas Havlik