PHP 中删除字符串中所有类型的制表符、空格、回车符

for*_*old -1 php

我有这样的文本数据:

                Learn More

        Hide [x]








                            Colors



                    Fetching Colors description...




            Show more topics







                            Art exhibitions



                    Fetching Art exhibitions description...








                            Abstract art



                    Fetching Abstract art description...








                            Representational art



                    Fetching Representational art description...
Run Code Online (Sandbox Code Playgroud)

我希望能够删除所有回车符,所以它变成这样:

Learn More Hide [x] Colors Fetching Colors description... Show more topics
Run Code Online (Sandbox Code Playgroud)

我如何在 PHP 中执行此操作?请注意,字符串可能是 UTF-8 字符串,我们需要考虑各种制表符、回车符和空格字符。(我正在思考编译器中使用的标记器算法,其中处理多个回车符和空格字符。)

eli*_*epc 5

您可以使用 preg_replace 尝试此操作:

http://php.net/manual/en/function.preg-replace.php

$data = preg_replace('/[\s\t\n]{2,}/', ' ', $data);
Run Code Online (Sandbox Code Playgroud)

您可以在以下位置了解有关正则表达式的更多信息: https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

您可以在这里实时测试它们: https: //www.phpliveregex.com/