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

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

本文介绍了使用正则表达式修剪多行字符串并缩小空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php函数 创建一个函数来修剪多行字符串中所有不必要的空格.

I want to create a function to trim all unnecessary white spaces from a multi line string.

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

The regex that it's not working is the one that removes spaces at the end:

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

从文本区域给出以下字符串:

Given the following string from a textarea:

 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

每行的开头和结尾都有空格,单词之间也有多个空格.

There are blank spaces at the beginning and end of each line plus more than one between the words.

运行函数后:

$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

为什么只从最后一行删除尾随空格?

Why is it only removing the trailing space from the last line?

推荐答案

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

You may redefine where $ matches using the (*ANYCRLF) verb.

请参见以下 PHP演示:

$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+ 水平空白字符
  • $-行尾(现在,在CR或LF之前)
  • ~m-启用多行模式($在行尾匹配).
  • (*ANYCRLF) - specifies a newline convention: (*CR), (*LF) or (*CRLF)
  • \h+ - 1+ horizontal whitespace chars
  • $ - end of line (now, before CR or LF)
  • ~m - multiline mode on ($ matches at the end of a line).

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

If you want to allow $ to match at any Unicode line breaks, replace (*ANYCRLF) with (*ANY).

请参见 PCRE参考中的 Newline conventions :

See Newline conventions in the PCRE reference:

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

现在,如果您需要

  • 修剪开始和结束的行
  • 将行内的空格缩小为一个空格

使用

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

请参见 PHP演示.

这篇关于使用正则表达式修剪多行字符串并缩小空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:25