从字符串中删除所有空白字符

发布于 2024-08-02 03:18:05 字数 525 浏览 7 评论 0原文

我知道 此评论 PHP.net< /em>。

我想要一个类似的工具,例如 tr对于 PHP,这样我就可以简单地运行

tr -d " " ""

我无法成功运行函数 php_strip_whitespace

$tags_trimmed = php_strip_whitespace($tags);

我运行正则表达式函数也失败了

$tags_trimmed = preg_replace(" ", "", $tags);

I know this comment on PHP.net.

I would like to have a similar tool like tr for PHP such that I can run simply

tr -d " " ""

I run unsuccessfully the function php_strip_whitespace by

$tags_trimmed = php_strip_whitespace($tags);

I run the regex function also unsuccessfully by

$tags_trimmed = preg_replace(" ", "", $tags);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

仅冇旳回忆 2024-08-09 03:18:05

要去除任何空格,您可以使用正则表达式

$str=preg_replace('/\s+/', '', $str);

另请参阅此答案了解可以的内容处理 UTF-8 字符串中的空格。

To strip any whitespace, you can use a regular expression

$str=preg_replace('/\s+/', '', $str);

See also this answer for something which can handle whitespace in UTF-8 strings.

忘东忘西忘不掉你 2024-08-09 03:18:05

默认情况下,正则表达式不考虑 UTF-8 字符。 \s 元字符仅说明原始拉丁语集。 因此,以下命令仅删除制表符、空格、回车符和换行符。

// http://stackoverflow.com/a/1279798/54964
$str=preg_replace('/\s+/', '', $str);

随着 UTF-8 成为主流,该表达式在到达新的 UTF-8 字符时将更频繁地失败/停止,从而在 \ 后面留下空格s 无法解释。

为了处理 Unicode/UTF-8 中引入的新型空白,需要更广泛的字符串来匹配和删除现代空白。

由于正则表达式默认不识别多字节字符,因此只能使用定界元字符串来识别它们,以防止字节段被其他 utf-8 字符(\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
);

这会考虑并删除制表符、换行符、垂直制表符、换页符、回车符、空格以及 此处

下一行、不间断空格、蒙古语元音分隔符、[en 四边形、em 四边形、en 空格、em 空格、三个空格、四个空格、六个空格、数字空格、标点符号空格、细空格、头发空格、零宽度空格、零宽度非连接符、零宽度连接符]、行分隔符、段落分隔符、窄不间断空格、中等数学空间、单词连接符、表意空格和零宽度不间断空格。

当从自动化工具或站点导出时,其中许多会对 XML 文件造成严重破坏,这些文件会扰乱文本搜索和识别,并且可以不可见地粘贴到 PHP 源代码中,从而导致解析器跳转到下一个命令(段落和行分隔符),这会导致代码行被跳过,从而导致间歇性的、无法解释的错误,我们已经开始将其称为“文本传播疾病”

(从网络复制和粘贴不再安全。使用字符扫描仪来保护您的代码。哈哈)

A regular expression does not account for UTF-8 characters by default. The \s meta-character only accounts for the original Latin set. Therefore, the following command only removes tabs, spaces, carriage returns and new lines

// http://stackoverflow.com/a/1279798/54964
$str=preg_replace('/\s+/', '', $str);

With UTF-8 becoming mainstream this expression will more frequently fail/halt when it reaches the new UTF-8 characters, leaving white spaces behind that the \s cannot account for.

To deal with the new types of white spaces introduced in Unicode/UTF-8, a more extensive string is required to match and removed modern white space.

Because regular expressions by default do not recognize multi-byte characters, only a delimited meta string can be used to identify them, to prevent the byte segments from being alters in other utf-8 characters (\x80 in the quad set could replace all \x80 sub-bytes in smart quotes)

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

This accounts for and removes tabs, newlines, vertical tabs, formfeeds, carriage returns, spaces, and additionally from here:

nextline, non-breaking spaces, mongolian vowel separator, [en quad, em quad, en space, em space, three-per-em space, four-per-em space, six-per-em space, figure space, punctuation space, thin space, hair space, zero width space, zero width non-joiner, zero width joiner], line separator, paragraph separator, narrow no-break space, medium mathematical space, word joiner, ideographical space, and the zero width non-breaking space.

Many of these wreak havoc in XML files when exported from automated tools or sites which foul up text searches, recognition, and can be pasted invisibly into PHP source code which causes the parser to jump to next command (paragraph and line separators) which causes lines of code to be skipped resulting in intermittent, unexplained errors that we have begun referring to as "textually transmitted diseases"

(It's not safe to copy and paste from the web anymore. Use a character scanner to protect your code. lol)

徒留西风 2024-08-09 03:18:05

有时您需要删除连续的空格。 你可以这样做:

$str = "My   name    is";
$str = preg_replace('/\s\s+/', ' ', $str);

输出:

My name is

Sometimes you would need to delete consecutive white spaces. You can do it like this:

$str = "My   name    is";
$str = preg_replace('/\s\s+/', ' ', $str);

Output:

My name is
世态炎凉 2024-08-09 03:18:05
$string = str_replace(" ", "", $string);

我相信 preg_replace 会寻找类似 [:space:] 的内容

$string = str_replace(" ", "", $string);

I believe preg_replace would be looking for something like [:space:]

绾颜 2024-08-09 03:18:05

您可以使用 php 中的 trim 函数来修剪两侧(左侧和右侧) )

 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

You can use the trim function from php to trim both sides (left and right)

 trim($yourinputdata," ");

Or

trim($yourinputdata);

You can also use

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

System: PHP 4,5,7

听,心雨的声音 2024-08-09 03:18:05

如果您想从 $tags 中删除所有空格,只需使用:

str_replace(' ', '', $tags);

如果您想删除新行等,则需要更多...

If you want to remove all whitespaces everywhere from $tags, just use:

str_replace(' ', '', $tags);

If you want to remove new lines and such that would require a bit more...

£烟消云散 2024-08-09 03:18:05

任何可能的选择是使用自定义文件包装器将变量模拟为文件。 您可以通过使用以下方法来实现:

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:// 协议上将任何文件函数与包装器一起使用(您也可以将其用于 include、require 等):

global $__myVar;
$__myVar = 'Enter tags here';
$data = php_strip_whitespace('var://__myVar');

注意:不要忘记将变量置于全局范围内(例如全局变量) $__myVar)

Any possible option is to use custom file wrapper for simulating variables as files. You can achieve it by using this:

1) First of all, register your wrapper (only once in file, use it like session_start()):

stream_wrapper_register('var', VarWrapper);

2) Then define your wrapper class (it is really fast written, not completely correct, but it works):

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) Then use any file function with your wrapper on var:// protocol (you can use it for include, require etc. too):

global $__myVar;
$__myVar = 'Enter tags here';
$data = php_strip_whitespace('var://__myVar');

Note: Don't forget to have your variable in global scope (like global $__myVar)

勿忘心安 2024-08-09 03:18:05

最短的答案:

strtr($str, [' '=>'']);

另一种“剥皮这只猫”的常见方法是使用爆炸和内爆,如下所示:

implode ('', 爆炸(' ', $str));

The shortest answer:

strtr($str, [' '=>'']);

Another common way to "skin this cat" would be to use explode and implode like this:

implode('', explode(' ', $str));

陌上芳菲 2024-08-09 03:18:05

基于这个被视为空格的字符表,简洁的模式可以使用 \pZ\pC 消除所有空格和控制字符。 这是这些正则表达式元字符的一个很好的参考

$sanitized = preg_replace('/[\pZ\pC]+/u', '', $rawText);

匹配正则表达式模式中所有空格的详细方法如下所示:

/
[
  \x{0009}-\x{000D}
  \x{0020}
  \x{0085}
  \x{00A0}
  \x{1680}
  \x{180E}
  \x{2000}-\x{200D}
  \x{2028}-\x{202F}
  \x{205F}
  \x{2060}
  \x{3000}
  \x{FEFF}
]+
/ux

演示

空格字符/ \s+//\s+/u/\pZ+/u/\pC+/u/[\pZ \pC]+/u
字符制表
换行
制表
回车
空格
下一行
不间断空格
奥格姆空格标记
蒙古元音分隔符
enquad
emquad
en space
全角间距
三格间距
四格间距
每格六格间距
图形间距
标点空格
细空格
头发空间
零宽度空格
零宽度非连接符
零宽度连接符
行分隔符
段落分隔符
窄不间断空格
中等数学空格
单词连接符
表意空格
零宽度不间断空格❌❌❌✅✅

Based on this table of characters that are considered whitespaces, a concise pattern can use \pZ and \pC to eliminate all of the whitespace and control characters. Here is a great reference to these regex metacharacters.

$sanitized = preg_replace('/[\pZ\pC]+/u', '', $rawText);

The verbose way of matching all of whitespaces in a regex pattern can look like this:

/
[
  \x{0009}-\x{000D}
  \x{0020}
  \x{0085}
  \x{00A0}
  \x{1680}
  \x{180E}
  \x{2000}-\x{200D}
  \x{2028}-\x{202F}
  \x{205F}
  \x{2060}
  \x{3000}
  \x{FEFF}
]+
/ux

Demo

whitespace characters/\s+//\s+/u/\pZ+/u/\pC+/u/[\pZ\pC]+/u
character tabulation
line feed
line tabulation
form feed
carriage return
space
next line
no-break space
ogham space mark
mongolian vowel separator
en quad
em quad
en space
em space
three-per-em space
four-per-em space
six-per-em space
figure space
punctuation space
thin space
hair space
zero width space
zero width non-joiner
zero width joiner
line separator
paragraph separator
narrow no-break space
medium mathematical space
word joiner
ideographic space
zero width non-breaking space
度的依靠╰つ 2024-08-09 03:18:05
$string = trim(preg_replace('/\s+/','',$string));
$string = trim(preg_replace('/\s+/','',$string));
荭秂 2024-08-09 03:18:05

您还可以使用 preg_replace_callback() 函数。 该函数与其同级函数 preg_replace() 相同,除了它可以采用回调函数之外,它可以让您更好地控制如何操作输出。

$str = "this is a   string";

echo preg_replace_callback(
        '/\s+/',
        function ($matches) {
            return "";
        },
        $str
      );

You could also use the preg_replace_callback() function. And this function is identical to its sibling preg_replace(), except for it can take a callback function which gives you more control on how you manipulate your output.

$str = "this is a   string";

echo preg_replace_callback(
        '/\s+/',
        function ($matches) {
            return "";
        },
        $str
      );
记忆之渊 2024-08-09 03:18:05

您可以使用 ereg_replace 来完成此操作

 $str = 'This Is New Method Ever';
 $newstr = ereg_replace([[:space:]])+', '',  trim($str)):
 echo $newstr
 // Result - ThisIsNewMethodEver

对于最新版本,您可以使用

$tags_trimmed = preg_replace('/\s+/', '', $tags);
$tags_trimmed = str_replace(' ', '', $tags);

By using ASCII

$whitespaces = array(chr(32), chr(9), chr(10), chr(13));
$tags_trimmed = str_replace($whitespaces, '', $tags);

 - Space: chr(32)
 - Tab: chr(9)
 - Newline: chr(10)
 - Carriage return: chr(13)

You can do it by using ereg_replace

 $str = 'This Is New Method Ever';
 $newstr = ereg_replace([[:space:]])+', '',  trim($str)):
 echo $newstr
 // Result - ThisIsNewMethodEver

For the newest version, you can use

$tags_trimmed = preg_replace('/\s+/', '', $tags);
$tags_trimmed = str_replace(' ', '', $tags);

By using ASCII

$whitespaces = array(chr(32), chr(9), chr(10), chr(13));
$tags_trimmed = str_replace($whitespaces, '', $tags);

 - Space: chr(32)
 - Tab: chr(9)
 - Newline: chr(10)
 - Carriage return: chr(13)
梦幻的味道 2024-08-09 03:18:05

从整个字符串中删除空格的一个简单方法是使用explode函数并使用for循环打印整个字符串。

 $text = $_POST['string'];
            $a=explode(" ", $text);
            $count=count($a);
            for($i=0;$i<$count; $i++){

                echo $a[$i];
            }

A simple way to remove spaces from the whole string is to use the explode function and print the whole string using a for loop.

 $text = $_POST['string'];
            $a=explode(" ", $text);
            $count=count($a);
            for($i=0;$i<$count; $i++){

                echo $a[$i];
            }
小忆控 2024-08-09 03:18:05

标签形式有一些特殊类型的空白。
您需要使用

$str=strip_tags($str);

删除多余标签、错误标签,以首先获得正常字符串。

并使用

$str=preg_replace('/\s+/', '', $str);

这对我有用。

There are some special types of whitespace in the form of tags.
You need to use

$str=strip_tags($str);

to remove redundant tags, error tags, to get to a normal string first.

And use

$str=preg_replace('/\s+/', '', $str);

It's work for me.

耶耶耶 2024-08-09 03:18:05

可以这样完成:

if(!function_exists('strim')) :
function strim($str, $charlist=" ", $option=0) {
    $return = '';
    if(is_string($str))
    {
        // Translate HTML entities
        $return = str_replace(" ", " ", $str);
        $return = strtr($return, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
        // Choose trim option
        switch($option)
        {
            // Strip whitespace (and other characters) from the begin and end of string
            default:
            case 0:
                $return = trim($return, $charlist);
            break;
            // Strip whitespace (and other characters) from the begin of string 
            case 1:
                $return = ltrim($return, $charlist);
            break;
            // Strip whitespace (and other characters) from the end of string 
            case 2:
                $return = rtrim($return, $charlist);
            break;
                
        }
    }
    return $return;
}
endif;

标准的 trim() 函数可以当 HTML 实体出现时就会出现问题。 这就是为什么我写了一个“Super Trim”函数来处理这个问题,你也可以选择从字符串的开头、结尾或两侧进行修剪。

It can be done like this:

if(!function_exists('strim')) :
function strim($str, $charlist=" ", $option=0) {
    $return = '';
    if(is_string($str))
    {
        // Translate HTML entities
        $return = str_replace(" ", " ", $str);
        $return = strtr($return, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
        // Choose trim option
        switch($option)
        {
            // Strip whitespace (and other characters) from the begin and end of string
            default:
            case 0:
                $return = trim($return, $charlist);
            break;
            // Strip whitespace (and other characters) from the begin of string 
            case 1:
                $return = ltrim($return, $charlist);
            break;
            // Strip whitespace (and other characters) from the end of string 
            case 2:
                $return = rtrim($return, $charlist);
            break;
                
        }
    }
    return $return;
}
endif;

The standard trim() functions can be a problematic when come HTML entities. That's why I wrote a "Super Trim" function what is used to handle this problem and also you can choose is trimming from the beginning, end or both sides of the string.

饮湿 2024-08-09 03:18:05

\s 正则表达式参数与每个系统上的所有 UTF-8 多字节字符串不兼容,即使与 /u 配对也是如此。

注意:这种方法是我几年前提出的,当时我尝试了当时发布的所有其他方法,发现它们不适用于我当时工作中使用的平台以及提供给我的数据集。 如果其他人发现自己处于常见答案不适用的情况,也许这个答案可能会有所帮助。

这个 PHP 正则表达式是我为了解决这个问题而编写的,它使用基于 PCRE (Perl 兼容正则表达式)参数作为 UTF-8 字符串的替换:

function remove_utf8_whitespace($string) {
   return preg_replace('/\h+/u','',preg_replace('/\R+/u','',$string));
}

- 用法示例 -

之前:

$string = " this is a test \n and another test\n\r\t ok! \n";

echo $string;

结果:

 this is a test
 and another test
         ok!

并且:

echo strlen($string); // result: 43

之后:

$string = remove_utf8_whitespace($string);

echo $string;

结果:

thisisatestandanothertestok!

并且:

echo strlen($string); // result: 28

PCRE 参数列表

来源:https://www.rexegg.com/regex-quickstart .html

Character  Legend                     Example    Sample Match
\t         Tab                        T\t\w{2}   T     ab
\r         Carriage return character  See below
\n         Line feed character        See below
\r\n       Line separator on Windows  AB\r\nCD   AB    CD

\N    Perl, PCRE (C, PHP, R…): one character that is not a line break    \N+    ABC
\h    Perl, PCRE (C, PHP, R…), Java: one horizontal whitespace character: tab or Unicode space separator
\H    One character that is not a horizontal whitespace
\v    .NET, JavaScript, Python, Ruby: vertical tab
\v    Perl, PCRE (C, PHP, R…), Java: one vertical whitespace character: line feed, carriage return, vertical tab, form feed, paragraph or line separator
\V    Perl, PCRE (C, PHP, R…), Java: any character that is not a vertical whitespace
\R    Perl, PCRE (C, PHP, R…), Java: one line break (carriage return + line feed pair, and all the characters matched by \v)

The \s regex argument is not compatible with all UTF-8 multi-byte strings on every system even when paired with /u.

NOTE: This method I came up with years ago after attempting all other methods posted here at the time and finding they did not work on the platform used at my job back then with the data set that was provided to me. If someone else finds themselves in a situation where the common answers don't apply, perhaps maybe this one might help.

This PHP regular expression is one I wrote to solve this using PCRE (Perl Compatible Regular Expressions) based arguments as a replacement for UTF-8 strings:

function remove_utf8_whitespace($string) {
   return preg_replace('/\h+/u','',preg_replace('/\R+/u','',$string));
}

- Example Usage -

Before:

$string = " this is a test \n and another test\n\r\t ok! \n";

echo $string;

Result:

 this is a test
 and another test
         ok!

And:

echo strlen($string); // result: 43

After:

$string = remove_utf8_whitespace($string);

echo $string;

Result:

thisisatestandanothertestok!

And:

echo strlen($string); // result: 28

PCRE Argument Listing

Source: https://www.rexegg.com/regex-quickstart.html

Character  Legend                     Example    Sample Match
\t         Tab                        T\t\w{2}   T     ab
\r         Carriage return character  See below
\n         Line feed character        See below
\r\n       Line separator on Windows  AB\r\nCD   AB    CD

\N    Perl, PCRE (C, PHP, R…): one character that is not a line break    \N+    ABC
\h    Perl, PCRE (C, PHP, R…), Java: one horizontal whitespace character: tab or Unicode space separator
\H    One character that is not a horizontal whitespace
\v    .NET, JavaScript, Python, Ruby: vertical tab
\v    Perl, PCRE (C, PHP, R…), Java: one vertical whitespace character: line feed, carriage return, vertical tab, form feed, paragraph or line separator
\V    Perl, PCRE (C, PHP, R…), Java: any character that is not a vertical whitespace
\R    Perl, PCRE (C, PHP, R…), Java: one line break (carriage return + line feed pair, and all the characters matched by \v)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文