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 right answer.
function removeMultipleBlankLines(&$array)
{
$value = $array;
$value = preg_replace("/(^[
]*|[
]+)[\s\t]*[
]+/", "
", $value);
return $value;
}
$textarray=array("The red","big"," "," ","fox","is ready","","","to jump.");
echo print_r(removeMultipleBlankLines($textarray));
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:
$string = str_replace("
",'',$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
also put a trim on it:
function removeMultipleBlankLines(&$array) {
return trim(preg_replace("/(^[
]*|[
]+)[\s\t]*[
]+/", "
", $array));
}
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.
Try this one:
$str =preg_replace("/(^[
]*|[
]+)[\s\t]*[
]+/", "
", $str);
The output file will give same output on simple notepad and also on text editors eg Notepad++.