Удалить пустую <> - PullRequest
       45

Удалить пустую <>

0 голосов
/ 28 октября 2019

Есть ли способ удалить < >, если он пуст?

текст может быть;< > 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 <b>unknown printer</b> took <> a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries. < >

вывод должен быть;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 <b>unknown printer</b> took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries.

1 Ответ

1 голос
/ 28 октября 2019

Вы можете использовать preg_replace:

$text = "<  > 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 <b>unknown printer</b> took <> a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries. <     >";
$text = preg_replace('/<\s*\>/', '', $text);
echo $text;

Вывод:

 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 <b>unknown printer</b> took  a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries. 

Демонстрация на 3v4l.org

Если вы также хотите очистить несколько или висячих пробелов (в начале или конце строки) от удаления пустых <>, вы можете использовать это вместо:

$text = preg_replace(array('/<\s*\>/', '/\s+/', '/^\s|\s$/'), array('', ' ', ''), $text);

Демо на 3v4l.org

...