2016-10-27 3 views
2

У меня есть строка, которая выглядит точно так же, как это:Удалить Странные Разрывы строки - 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. 

Есть ли способ сделать это выглядит в 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. 

Где целая фраза находится на одной линии? И абзацы сохраняются?

Обновление: Я нашел этот инструмент, который делает это отлично. Но, очевидно, его нельзя использовать. http://www.textfixer.com/tools/remove-line-breaks.php

+0

дать этому попытку: 'str_replace (массив ("\ г", "\ п"), '', $ строка)' где '\ r' = return и' \ n' = новая строка. поэтому он удалит все строки возврата и строки новой строки из строки. –

+0

Попробуйте preg_replace («/ \ r | \ n /», «", $ yourString); но если вы смотрите на скорость, str_replace (array ("\ r", "\ n"), '', $ yourString; это ваш лучший выбор –

+0

@mistermartin Он удаляет все разрывы строк, но не разделяет абзацы. – Taurian

ответ

2

на основе вашего ответа на мой комментарий, вам может потребоваться поэкспериментировать с новыми строками \n и возвращает \r в зависимости от того, что содержит ваша фактическая строка. но следующие работы для меня:

$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>'; 

выход:

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

Это замечательно. Благодаря! – Taurian

-1

Вы можете попробовать заменить каждое многократное пространство и/или новую строку одним пространством. Это будет выглядеть примерно так:

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

Надеется, что это работает.

Смежные вопросы