简体   繁体   中英

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 one 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;

尝试这个

preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM