通过PHP从文本中删除单个换行符

时间:2013-10-01 18:19:12

标签: php regex validation text data-cleansing

我从Web源获取数据,该数据源返回的数据通常格式不佳。我想清理文本,这样就会返回:

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

我想删除单行换行符,但保留双行换行符。如何在PHP中完成?理想情况下,字符串最终会如下所示:

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

我尝试过preg_replace( "/\r|\n/", "", $string);,但这会删除双线和单线。

2 个答案:

答案 0 :(得分:1)

试试这个正则表达式:

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

regex101 demo

答案 1 :(得分:0)

你可以试试这个:

$result = preg_replace('~(?<!\n)\h*+\r?\n\h*+(?!\r?\n)~', ' ', $text);