How do I remove multiple blank lines from a string. I have looked at the examples on stackoverflow and have tried to change my code accordingly but I am not getting the rig
also put a trim on it:
function removeMultipleBlankLines(&$array) {
return trim(preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $array));
}
try this:
$string = str_replace("\r\n",'',$string);
why not $string = trim($string);
?
you can even specify what you want to be trimmed with a second optional parameter, that is trim ( string $str [, string $charlist ] )
see the docs http://php.net/manual/en/function.trim.php
You can do:
$array_with_nonblank_lines = array_filter($textarray, 'trim');
to get exactly what you want.
Here is link to php's docs page about trim function, which does the job.
Try this one:
$str =preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\r\n", $str);
The output file will give same output on simple notepad and also on text editors eg Notepad++.
Using regular expressions works, but some people unfamiliar with regular expressions could get confused as to how you lay out your expression. Using the familiar trim command is simply a better choice in my opinion, but in the end I think trim would be faster because it doesn't need to evaluate the regular expression etc.