Multiple blank lines with spaces, tabs and new lines to one line?
I know there are similar questions, but i have certain different problems!
I need to convert multiple blank lines to o开发者_如何学JAVAne in PHP, but when I use not only New Lines, but Spaces and New Lines it doesn't work. If there are 2 or more blank lines(with spaces and enter's), they should be converted in only one blank line, when it's only new line(but not blank line), it should remain the same.
I used this, but it doesn't work with spaces and new lines:
$string = str_replace(array("\r\n", "\r", "\n", "<br />", $string);
echo preg_replace("~<br />(<br\s*/?>[\r\n]*)+~i", "<br /><br />", $string);
I also need it to be displayed in this way in Windows, Linux and Mac!
I think these are the only things you need
$str = str_replace(array("\n", "\r", "\t"), ' ', $str);
$str = preg_replace('/(\s+)/', ' ', $str);
Try following
<?php
$str = '
aa
a
a
a
';
for($i = 1;$i< 8;$i++){
$str = str_replace("\r\n\r\n\r\n", "\r\n\r\n", $str);
$str = str_replace("\t", '', $str);
}
echo $str;
try this
preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
精彩评论