我想要一个类似的工具,就像PHP中的
tr
一样,这样我就可以简单地运行。tr -d " " ""
我运行了函数
php_strip_whitespace
但没有成功。$tags_trimmed = php_strip_whitespace($tags);
我也尝试运行正则表达式函数,但不成功。
$tags_trimmed = preg_replace(" ", "", $tags);
tr
一样,这样我就可以简单地运行。tr -d " " ""
php_strip_whitespace
但没有成功。$tags_trimmed = php_strip_whitespace($tags);
$tags_trimmed = preg_replace(" ", "", $tags);
+
放在 s
后面?对于这个你不需要它。 - Buttle Butkus\s
只能匹配原始的拉丁字符集。因此,以下命令只能移除制表符、空格、回车和换行符。// https://dev59.com/YHM_5IYBdhLWcg3wq1GF#1279798
$str=preg_replace('/\s+/', '', $str);
\s
识别的空白字符。\x80
替换(四元组中的\x80
可以替换智能引号中的所有\x80
子字节)。$cleanedstr = preg_replace(
"/(\t|\n|\v|\f|\r| |\xC2\x85|\xc2\xa0|\xe1\xa0\x8e|\xe2\x80[\x80-\x8D]|\xe2\x80\xa8|\xe2\x80\xa9|\xe2\x80\xaF|\xe2\x81\x9f|\xe2\x81\xa0|\xe3\x80\x80|\xef\xbb\xbf)+/",
"_",
$str
);
下一行、不间断空格、蒙古语元音分隔符,[四分之一em空格、半个em空格、一个em空格、一个en空格、三分之一em空格、四分之一em空格、六分之一em空格、数字空格、标点空格、窄空格、短空格、零宽空格、零宽非连接符、零宽连接符],行分隔符、段落分隔符、窄不换行空格、中等数学空格、词连接符、表意空格,以及零宽不换行空格。
许多这些字符在从自动化工具或网站导出的XML文件中会造成混乱,影响文本搜索和识别,并且可能被无形地粘贴到PHP源代码中,导致解析器“跳到下一个命令”(段落和行分隔符),从而导致代码行被跳过,引发间歇性的、无法解释的错误,我们开始称之为“文本传播疾病”。
(从网页上复制粘贴已经不安全了。使用字符扫描器来保护您的代码。哈哈)
trim()
应该是正确的答案。 - Husamu
标志? - user5147563有时您需要删除连续的空格。您可以这样做:
$str = "My name is";
$str = preg_replace('/\s\s+/', ' ', $str);
输出:
My name is
/\s{2,}/
,但你的模式和我的模式不能将一个制表符标准化为一个空格。也许更好的做法是消耗所有出现一次或多次的空白字符。/\s+/
- undefined$string = str_replace(" ", "", $string);
我认为 preg_replace 会寻找像 [:space:]
这样的内容
trim($yourinputdata," ");
或者
trim($yourinputdata);
您还可以使用
ltrim() - Removes whitespace or other predefined characters from the left side of a string
rtrim() - Removes whitespace or other predefined characters from the right side of a string
系统:PHP 4、5、7
文档:http://php.net/manual/zh/function.trim.php
如果您想从$tags中完全删除所有空格,为什么不直接使用以下方法:
str_replace(' ', '', $tags);
如果您想去除换行符等,那就需要做更多的工作...
有可能的解决方案是使用自定义文件包装器来模拟变量作为文件。您可以通过使用以下方式实现:
1)首先,在文件内注册您的包装器(仅在文件中注册一次,就像 session_start() 一样使用它):
stream_wrapper_register('var', VarWrapper);
2) 然后定义您的包装类(它写得非常快,不完全正确,但它可以工作):
class VarWrapper {
protected $pos = 0;
protected $content;
public function stream_open($path, $mode, $options, &$opened_path) {
$varname = substr($path, 6);
global $$varname;
$this->content = $$varname;
return true;
}
public function stream_read($count) {
$s = substr($this->content, $this->pos, $count);
$this->pos += $count;
return $s;
}
public function stream_stat() {
$f = fopen(__file__, 'rb');
$a = fstat($f);
fclose($f);
if (isset($a[7])) $a[7] = strlen($this->content);
return $a;
}
}
3) 然后使用任何文件函数与您的 var:// 协议包装器(您也可以将其用于包括、要求等):
global $__myVar;
$__myVar = 'Enter tags here';
$data = php_strip_whitespace('var://__myVar');
global $$varname;
在2013年是多么可怕。 - B001ᛦ这是一篇旧文章,但最短的答案没有列在这里,所以我现在添加它。
strtr($str,[' '=>'']);
另一种常见的方法是使用explode和implode来实现:
implode('',explode(' ', $str));
你还可以使用preg_replace_callback
函数。这个函数与它的兄弟函数preg_replace
相同,除了它可以接受一个回调函数,让你对输出进行更多的控制。
$str = "this is a string";
echo preg_replace_callback(
'/\s+/',
function ($matches) {
return "";
},
$str
);
preg_replace
的回调函数在哪些情况下有用? - Léo Léopold Hertz 준영preg_replace()
完全能够用空字符串替换匹配到的字符串,所以没有什么好的理由使用preg_replace_callback()
来解决这个问题。 - undefined$string = trim(preg_replace('/\s+/','',$string));
trim()
方法是多余的——正则表达式模式会在trim()
有机会之前消除字符串中的所有空格。这个没有解释的代码片段对这个页面没有任何新的价值。 - undefined