user1797675
user1797675

Reputation:

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

Upvotes: 2

Views: 175

Answers (2)

mister martin
mister martin

Reputation: 6252

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."

Upvotes: 1

KristiyanB
KristiyanB

Reputation: 3

You could try replacing every multiple space and/or newline with a single space. It would look something like:

$string = trim(preg_replace('/\s\s+/', ' ', $string));

Hope it works.

Upvotes: 0

Related Questions