Pergunta

I'm fetching data from a web source which returns the data often poorly-formatted. I want to clean up the text, that would be returned like so:

Featuring six amazing National Parks: Glacier,
Waterton Lakes, Yellowstone, Grand Teton, Arches, and Canyonlands.

Your tour begins in Chicago where you will board
the Empire Builder
bound for the first National Park on your trip, Glacier, where you will explore the famed Going-to-the-Sun Road and enjoy a full-day excursion to
Waterton Lakes National Park in Canada (passport required).

I want to strip the single linebreaks, but retain the double ones. How can this be done in PHP? Ideally the string would end up looking as follows:

Featuring six amazing National Parks: Glacier, Waterton Lakes, Yellowstone, Grand Teton, Arches, and Canyonlands.

Your tour begins in Chicago where you will board the Empire Builder bound for the first National Park on your trip, Glacier, where you will explore the famed Going-to-the-Sun Road and enjoy a full-day excursion to Waterton Lakes National Park in Canada (passport required).

I have tried preg_replace( "/\r|\n/", "", $string); but this removes double-linebreaks as well as single ones.

Foi útil?

Solução

Try this regex:

preg_replace('/(?<!\n) *\n(?=[^\n])/', " ", $string);

regex101 demo.

Outras dicas

You can try this:

$result = preg_replace('~(?<!\n)\h*+\r?\n\h*+(?!\r?\n)~', ' ', $text);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top