修剪 使用 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2521051/
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
Trim with PHP
提问by spotlightsnap
I have a sentence like this.
我有一个这样的句子。
1 2 3 4
As you see, in between 1 2 and 3 text, there are extra spaces. I want the output with only one space between them. so my output will be 1 2 3 4.
如您所见,在 1 2 和 3 文本之间,有额外的空格。我希望输出之间只有一个空格。所以我的输出将是1 2 3 4.
If I use trim, it can only remove white space, but not that How can I use PHP trim function to get the output like this?
如果我使用修剪,它只能删除空格,但不能删除 如何使用 PHP 修剪函数来获得这样的输出?
回答by Tobias Fünke
Found this at php.net, works great:
在 php.net 上找到了这个,效果很好:
$myHTML = " abc";
$converted = strtr($myHTML, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
trim($converted, chr(0xC2).chr(0xA0));
回答by Lotus Notes
$str = "1 $nbsp; 2 3 4";
$new_str = str_replace(" ", '', $str);
回答by Chaoix
A more inclusive answer for those who want to just do a trim:
对于那些只想做修剪的人来说,一个更具包容性的答案:
$str = trim($str, " \t\n\r$str = trim(html_entity_decode($str), " \t\n\r$str = " abc ";
echo trim($str, "\xC2\xA0"); //abc
\x0B\xC2\xA0");
\x0B\xC2\xA0");
Same trim handling html entities:
相同的修剪处理 html 实体:
function clean($str)
{
$str = utf8_decode($str);
$str = str_replace(" ", " ", $str);
$str = preg_replace('/\s+/', ' ',$str);
$str = trim($str);
return $str;
}
$html = "1 $nbsp; 2 3 4";
$output = clean($html);
echo $output;
This html_entity_decode and trim interaction is outlined in the PHP docs here: http://php.net/manual/en/function.html-entity-decode.php#refsect1-function.html-entity-decode-notes
此处的 PHP 文档概述了 html_entity_decode 和修剪交互:http: //php.net/manual/en/function.html-entity-decode.php#refsect1-function.html-entity-decode-notes
回答by Kenny
$str="1 2 3 4";
$s = str_replace(" ","",$str);
print $s;
回答by Adam Ranganathan
A little late to answer but hopefully might help someone else. The most important 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. The following function replaces with a space. Then all extra white spaces are replaced with a single white space using preg_replace(). Leading and trailing white spaces are removed in the end.
回答有点晚,但希望可以帮助别人。从 html 中提取内容最重要的是在 php 中使用utf8_decode()。然后所有其他字符串操作变得轻而易举。甚至可以通过直接从浏览器复制粘贴字符到php代码中来替换外来字符。以下函数替换 为空格。然后,所有额外的空格都使用preg_replace(). 最后删除前导和尾随空格。
echo str_replace ( " ", "", "1 2 3 4" );
1 2 3 4
1 2 3 4
回答by ghostdog74
if your string actually has " ",
如果你的字符串实际上有“”,
##代码##回答by Steve Obbayi
just remember you need to echo out the result of the str_replace and you alo dont need to worry about white spaces a the browser will only show one white space.
请记住,您需要回显 str_replace 的结果,而且您不必担心空格,因为浏览器只会显示一个空格。

