php preg_replace:如何去除所有空白和  ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3789204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:07:28  来源:igfitidea点击:

preg_replace: how do I strip off all white space and  ?

phpregexpreg-replacewhitespace

提问by laukok

how do I strip off all white space and  ?

我如何去除所有空白和 

I have this as a input in a wrapper I create,

我将此作为我创建的包装器中的输入,

[b]       bold       [/b]

[b]       bold       [/b]

so before turning the text to bold, i want to strip off all white spaces and &nbsp, and turn it into [b]bold[/b],

所以在将文本变成粗体之前,我想去掉所有的空格和  ,然后把它变成[b]bold[/b]

$this->content = preg_replace("/\[(.*?)\]\s\s+(.*?)\s\s+\[\/(.*?)\]/", 
                "[][/]", 
                $this->content);

but it does not work! can you help please?

但它不起作用!你能帮忙吗?

回答by codaddict

There is no need for a regex based solution. You can simply use str_replaceas:

不需要基于正则表达式的解决方案。您可以简单地str_replace用作:

$input = "[b]       bold       [/b]";
$input = str_replace(array(' ',' '),'',$input);
echo trim($input); // prints [b]bold[/b]

回答by laukok

I found another solution

我找到了另一个解决方案

$this->content = preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[][/]", html_entity_decode($this->content));

回答by NikiC

$this->content = preg_replace(
    '~\[(.*?)](?:\s| )*(.*?)(?:\s| )*\[/\1]/', 
    '[][/]', 
    $this->content
);

回答by mkb

You can just replace the spaces with empty strings, e.g.

您可以用空字符串替换空格,例如

preg_replace("/(?:\s| )+/", "", $this->content, -1)

The -1 causes the replace to hit every instance of the match.

-1 导致替换命中匹配的每个实例。

回答by Adam Ranganathan

A little late to answer but hopefully might help someone else. The most important thing while extracting content from html is to use utf8_decode()in php. Then all other string operations become a breeze. Even foreign characters can be replaced by directly copy pasting characters from browser into the php code.

回答有点晚,但希望可以帮助别人。从 html 中提取内容时最重要的是在 php 中使用utf8_decode()。然后所有其他字符串操作变得轻而易举。甚至可以通过直接从浏览器复制粘贴字符到php代码中来替换外来字符。

The following function replaces  with empty character. Then all white spaces are replaced with empty character using preg_replace().

以下函数替换 为空字符。然后使用preg_replace().将所有空格替换为空字符。

function clean($str)
{       
    $str = utf8_decode($str);
    $str = str_replace(" ", "", $str);
    $str = preg_replace("/\s+/", "", $str);
    return $str;
}

$html = "[b]       bold       [/b]";
$output = clean($html);
echo $output;

[b]bold[/b]

[b]粗体[/b]

回答by David

Another method that would work is:

另一种可行的方法是:

$this->content = trim(str_replace(' ','',$this->content));

PHP Manual links:

PHP手册链接:

trim() http://us.php.net/trim

修剪()http://us.php.net/trim

*note: This is assuming $this->content contains only the string posted by OP

*注意:这是假设 $this->content 只包含 OP 发布的字符串

回答by poke

To give you a complete solution with regular expressions as well:

为了给你一个完整的正则表达式解决方案:

$this->content = preg_replace( '/\[([a-z]+)\](?: |\s)*(.*?)(?: |\s)*\[\/([a-z]+)\]/', '[][/]', $this->content );

But in this case you should rather combine the whitespace removal and bbcode transformation to make sure that the tags are correctly:

但在这种情况下,您应该将空格删除和 bbcode 转换结合起来,以确保标签正确:

$this->content = preg_replace( '/\[b\](?:&nbsp;|\s)*(.*?)(?:&nbsp;|\s)*\[\/b]/', '<b></b>', $this->content );