Taurian Taurian - 1 month ago 16
PHP Question

Remove Strange Line Breaks - PHP

I have a string that looks exactly like this:

Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book.


Is there a way to make it looks like this in PHP?

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.


Where an entire sentence is on one line? And paragraphs are kept?

Update:
I found this tool that does it perfectly. But obviously can't be used.
http://www.textfixer.com/tools/remove-line-breaks.php

Answer

based on your response to my comment, you may need to experiment with newlines \n and returns \r depending on what your actual string contains. but the following works for me:

$str = "Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book.";

// replace all double newlines with breaks
$str = str_replace("\n\n", '<br />', $str);
// replace all single newlines with a space
$str = str_replace("\n", ' ', $str);
// replace all breaks with double newlines
$str = str_replace('<br />', "\n\n", $str);

echo '<pre>';
var_dump($str);
echo '<pre>';

output:

string(487) "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."