删除文本PHP中的多行

时间:2019-02-06 14:21:58

标签: php html tags line

当我尝试与之联系时,其外观如下 我想在这种情况下删除多行

        $contents= "MULTIPLE CHOICE
    1 : _____ is defined as getting work done through others.
    A : Orientation




    B : 



                                Marketing




    C : 



                                Management "; 

echo $contents;

我可以做很多事情,但是不起作用

$contents = preg_replace("/\/\/\n/", "\n",$contents);
$contents = trim(preg_replace('/\s\s+/', ' ', $contents));

和其他代码但不起作用

3 个答案:

答案 0 :(得分:0)

删除所有空白和所有换行符:

 $contents = preg_replace("/[\n\r ]/", "",$contents);

答案 1 :(得分:0)

您可以尝试做的一件事是查看换行符是否不是\ n,例如检查[\ r \ n]是否获得CR和换行符。

$contents = preg_replace("/[\r\n]+/", "\n", $contents);

+量词告诉模式匹配器重复一次或多次重复上一个项目。因此,它将连续获取所有其他功能,并替换为一个。

对于您的空间问题,您可以运行

$contents = preg_replace("/\s+/", " ", $contents);

This site has a lot of useful info on Regex

答案 2 :(得分:0)

感谢所有我找到的解决方案,最好的解决方案是

   // removing any none word characters after ': xxxxx' and replace it with ': '
// this will remove all spaces and line brakes after ':'
$contents = preg_replace('/:\W+/', ': ', $contents); 
// removing any multiple white spaces including line breaks and replace it with single line break
// this will remove all spaces and line brakes after the answer
$contents = preg_replace("/\s{2,}/", "\n", $contents);