trim 行并使用正则表达式缩小多行字符串的空格

trim lines and shrink whitespaces using regex for multi line string

正在使用 php function 想要创建一个函数来 trim 来自多行字符串的所有不必要的白色 space .

它不起作用的正则表达式是最后删除 spaces 的正则表达式:

// Always trim at the end. Warning: this seems to be the costlier
// operation, perhaps because looking ahead is harder?
$patterns[] = ['/ +$/m', ''];

给定文本区域中的以下字符串:

 first  line... abc   //<-- blank space here
 second  is  here... def   //<-- blank space here
 //<-- blank space here
 fourth  line... hi  there   //<-- blank space here

 sith  is  here....   //<-- blank space here

每行首尾有空格space加上字与字之间多了一个

我运行函数后:

$functions->trimWhitespace($description, ['blankLines' => false]);

这是我得到的:

first line... abc //<-- blank space here
second is here... def //<-- blank space here
//<-- no bank space here
fourth line... hi there //<-- blank space here

sith is here....//<-- no blank space here

为什么只删除最后一行的结尾 space?

preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

所以你想要preg_replace('/[\s]+$/m', '', $string)

使用两步法:

<?php

$text = " first  line... abc   
 second  is  here... def   
  <-- blank space here
 fourth  line... hi  there   

 sith  is  here....   ";

// get rid of spaces at the beginning and end of line
$regex = '~^\ +|\ +$~m';
$text = preg_replace($regex, '', $text);

 // get rid of more than two consecutive spaces
$regex = '~\ {2,}~';
$text = preg_replace($regex, ' ', $text);
echo $text;

?>

a demo on ideone.com

 preg_replace('/*(.*) +?\n*$/', $content)

Live Demo

你需要 /gm 而不仅仅是 /m

代码应该变成: (此代码无效,更新的代码有效)

$patterns[] = ['/ +$/mg', ''];

这里的工作示例:https://regex101.com/r/z3pDre/1

更新:

g标识符,不要这样用。我们需要将 preg_match 替换为 preg_match_all

使用不带 g 的正则表达式,如下所示:

$patterns[] = ['/ +$/m', ''];

您可以使用 (*ANYCRLF) 动词重新定义 $ 匹配的位置。

见下文PHP demo

$s = " ddd    \r\n  bbb     ";
$n = preg_replace('~(*ANYCRLF)\h+$~m', '', $s); // if the string can contain Unicode chars,
echo $n;                                        // also add "u" modifier ('~(*ANYCRLF)\h+$~um')

详情:

  • (*ANYCRLF) - 指定换行约定:(*CR)(*LF)(*CRLF)
  • \h+ - 1+ 水平 白色space 字符
  • $ - 行尾(现在,在 CR 或 LF 之前)
  • ~m - 多行模式开启($ 在行尾匹配)。

如果要允许 $ 匹配任何 Unicode 换行符,请将 (*ANYCRLF) 替换为 (*ANY)

参见 Newline conventions 中的 PCRE reference:

(*CR)        carriage return
(*LF)        linefeed
(*CRLF)      carriage return, followed by linefeed
(*ANYCRLF)   any of the three above
(*ANY)       all Unicode newline sequences

现在,如果你需要

  • Trim 开头和结尾的行
  • 将线条内的白色space缩小为一个space

使用

$s = " Ł    ę  d    \r\n  Я      ёb     ";
$n = preg_replace('~(*ANYCRLF)^\h+|\h+$|(\h){2,}~um', '', $s);
echo $n;

参见PHP demo