删除空的<>

时间:2019-10-28 00:55:09

标签: php php-7

如果< >为空,是否可以删除它?

文本可以是; < > 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 个答案:

答案 0 :(得分:1)

您可以使用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. 

Demo on 3v4l.org

如果您还想清除空<>的行中的任何多个或悬挂的空格(在行的开头或结尾),则可以使用以下代替:

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

Demo on 3v4l.org