除去多行文本PHP

问题描述 投票:-2回答:3

当我试图让联系它的外观像这样我想删除多线在这种情况下,

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

并且但是另外的代码不起作用

php html tags line
3个回答
0
投票

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

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

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


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);
© www.soinside.com 2019 - 2024. All rights reserved.