我从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);
,但这会删除双线和单线。
答案 0 :(得分:1)
答案 1 :(得分:0)
你可以试试这个:
$result = preg_replace('~(?<!\n)\h*+\r?\n\h*+(?!\r?\n)~', ' ', $text);