Removing empty lines with preg_replace
Just a quick post today to mention something that will come in useful if you need to parse or handle large amounts of text and whitespace.
I came across this when dealing with some HTML output from curl. The ouput was fine but the website that I was curling (not sure if thats a word :p) for one reason or another had alot of blank lines in its source. To make this easier to deal with a simple regex (regular expression) string can tidy it up with the use of preg_replace:
$html = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n\']+/", "\n", $html);
This gets all of the empty lines in the source and replaces them with a simple carriage return to preserve the content structure.