0

I'm trying to process a chunk of text in PHP to remove word wrapping. Think of this as a reverse wordwrap function that affects only lines that were broken in the middle, but keeps line breaks at the end of paragraphs. Original content is in plain text format.

This is an example of the original content:

The quick brown fox jumps
over the lazy dog. Foxes
are orange and dogs are blue.
A blue bird appeared on the
window, singing jolly songs.

It should be converted to this:

The quick brown fox jumps over the lazy dog. Foxes are orange and dogs are blue.

A blue bird appeared on the window, singing jolly songs.

My logic is to create a list of accepted end of line characters, like period, colon and semicolon, and remove any breaks from lines not ending with those characters. I think it works, but I'm having a hard time translating it into a regex. Any help would be appreciated.

My progress so far:
$content = preg_replace("/(?<!\.)$/m", "XXXX", $content);

This matches any line not ending with a period. I'd still have to include the line break on the match and any white space after the period. I think that I also need to create a group to also match other line ending characters, line colons and semicolons. Having a hard time putting it all together.

2
  • I've just updated the question with my progress so far. Thank you for pointing it out. Commented Feb 3, 2014 at 18:19
  • Would you be willing to go with a solution that takes two passes through your text? One pass to find acceptable end-of-line characters and then inserting an extra newline. Then, another pass to remove all newlines which are not immediately followed by another newline. Commented Feb 5, 2014 at 18:06

1 Answer 1

1

If you want to do it by regex you will have to look into lookbehind especially negative lookbehind, you can read a bit about it here.

You can build from this:

<?php
$data = file_get_contents('test_data.txt');
echo preg_replace("/\n(?<![.,;]\n)/"," ",$data);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.