正则表达式修剪或 preg_replace 空白,包括制表符和换行符

发布于 2025-01-01 23:20:26 字数 573 浏览 0 评论 0原文

如何使用 PHP 修剪“poo”之前的所有空白以及之后的所有空白?

我想把这个:变成

<code><div class="b-line"></div>  \t 

             \n\n\n \t \n   poo
<lol>
 n \n \n \t </code>

这个:

<code><div class="b-line"></div>poo 
<lol>
 n</code>

这部分将始终位于字符串的开头:

谢谢

编辑:抱歉,我应该解释一下,上面的全部内容都在一个字符串中,我只是想修剪紧接着 <之后的空白;div class="b-line"> 以及紧邻之前

How can I use PHP to trim all white space up to "poo" and all white space afterwards?

I would like to turn this:

<code><div class="b-line"></div>  \t 

             \n\n\n \t \n   poo
<lol>
 n \n \n \t </code>

In to this:

<code><div class="b-line"></div>poo 
<lol>
 n</code>

This part will always be at the start of the string: <code><div class="b-line"></div>

Thanks

Edit: Sorry I should explain that the whole of the above is in a string and I only want to trim the whitespace immediately after <code><div class="b-line"></div> and immediately before </code>

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

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

发布评论

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

评论(5

演多会厌 2025-01-08 23:20:26
$str = trim($str, "\t\n");

请参阅 修剪

$str = trim($str, "\t\n");

See trim

御弟哥哥 2025-01-08 23:20:26

preg_* 函数提供 空白转义序列 \s,您可以使用它,因此您的正则表达式将是:

$regexp = '~...>\\s*([^<]*?)\\s*<~m'

也许您需要使用 [\\s$] 而不仅仅是 \ \s,我不确定PCRE如何处理在这些情况下换行。

preg_* functions provides whitespace escape sequence \s, which you can use, so you're regex would be:

$regexp = '~...>\\s*([^<]*?)\\s*<~m'

Maybe you will need to use [\\s$] instead of just \\s, I'm nor sure how PCRE handles newlines in those cases.

情话墙 2025-01-08 23:20:26

我只想修剪紧接在

之后和紧接 之前的空白;

可以通过以下方式完成:

preg_replace(',(?|(<code><div class="b-line"></div>)\s+|\s+(</code>)),', '$1', $str);

此处示例


如果 标记仅出现在字符串的开头/结尾,您可能需要使用 ^$ 来锚定表达式:

(?|^(<code><div class="b-line"></div>)\s+|\s+(</code>)$)

I only want to trim the whitespace immediately after <code><div class="b-line"></div> and immediately before </code>

Can be done with:

preg_replace(',(?|(<code><div class="b-line"></div>)\s+|\s+(</code>)),', '$1', $str);

Example here.


If the <code> tag only occurs at beginning/end of string you would want to anchor the expression with ^ and $:

(?|^(<code><div class="b-line"></div>)\s+|\s+(</code>)$)
就是爱搞怪 2025-01-08 23:20:26

我认为这个基于前向和后向的正则表达式将适合您:

$str = <<< EOF
<code><div class="b-line"></div>  \t 

             \n\n\n \t \n   poo
<lol>
 n \n \n \t </code>
EOF;
$str = preg_replace_callback('#(?<=<code><div class="b-line"></div>)(.*?)(\s*<[^>]*>\s*)(.*?)(?=</code>)#is',
       create_function('$m',
       'return str_replace(array("\n", "\t", " "), "", $m[1]).$m[2].str_replace(array("\n", "\t", " "), "", $m[3]);'),
       $str);
var_dump ( $str );

输出:

string(51) "<code><div class="b-line"></div>poo
<lol>
 n</code>"

I think this lookahead and lookbehind based regex will work for you:

$str = <<< EOF
<code><div class="b-line"></div>  \t 

             \n\n\n \t \n   poo
<lol>
 n \n \n \t </code>
EOF;
$str = preg_replace_callback('#(?<=<code><div class="b-line"></div>)(.*?)(\s*<[^>]*>\s*)(.*?)(?=</code>)#is',
       create_function('$m',
       'return str_replace(array("\n", "\t", " "), "", $m[1]).$m[2].str_replace(array("\n", "\t", " "), "", $m[3]);'),
       $str);
var_dump ( $str );

OUTPUT:

string(51) "<code><div class="b-line"></div>poo
<lol>
 n</code>"
゛清羽墨安 2025-01-08 23:20:26

@Vyktor 的答案几乎是正确的。如果您只是针对字符串(即 $s)运行 echo preg_replace('/\s/', '', $s); 您将得到:

<code><divclass="b-line"></div>poo<lol>n</code>

测试片段:

  <?php
  $s = <<<EOF
  <code><div class="b-line"></div>




      poo
  <lol>
   n

  </code>      
  EOF;      
  echo preg_replace('/\s/', '', $s);      
  ?>

@Vyktor's answer is almost correct. If you just run echo preg_replace('/\s/', '', $s); against your string (which is $s) you will get:

<code><divclass="b-line"></div>poo<lol>n</code>

Test snippet:

  <?php
  $s = <<<EOF
  <code><div class="b-line"></div>




      poo
  <lol>
   n

  </code>      
  EOF;      
  echo preg_replace('/\s/', '', $s);      
  ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文