php ->preg_replace ->仅删除引号之间的空格 [英] php -> preg_replace -> remove space ONLY between quotes

查看:32
本文介绍了php ->preg_replace ->仅删除引号之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅删除引号之间的空格,例如:

I'm trying to remove space ONLY between quotes like:

$text = 'good with spaces "here all spaces should be removed" and here also good';

有人可以帮忙写一段有效的代码吗?我已经试过了:

can someone help with a working piece of code ? I already tried:

$regex = '/(\".+?\")|\s/';

$regex = '/"(?!.?\s+.?)/';

没有成功,我发现了一个方向错误的样本:-(删除空白字符,PHP 中的引号除外? 但我不能改变它.

without success, and I found a sample that works in the wrong direction :-( Removing whitespace-characters, except inside quotation marks in PHP? but I can't change it.

谢谢新人

推荐答案

preg_replace_callback 可以轻松解决此类问题.这个想法包括提取引号之间的子字符串,然后在回调函数中对其进行

This kind of problem are easily solved with preg_replace_callback. The idea consists to extract the substring between quotes and then to edit it in the callback function:

$text = preg_replace_callback('~"[^"]*"~', function ($m) {
    return preg_replace('~\s~', '#', $m[0]);
}, $text);

这是最简单的方法.

使用带有 preg_replace 的单个模式来实现更复杂,但它是可能的:

It's more complicated to do it with a single pattern with preg_replace but it's possible:

$text = preg_replace('~(?:\G(?!\A)|")[^"\s]*\K(?:\s|"(*SKIP)(*F))~', '#', $text);

演示

图案详情:

(?:
    \G (?!\A)  # match the next position after the last successful match
  |
    "          # or the opening double quote
)
[^"\s]*        # characters that aren't double quotes or a whitespaces
\K             # discard all characters matched before from the match result
(?:
    \s         # a whitespace
  |
    "           # or the closing quote
    (*SKIP)(*F) # force the pattern to fail and to skip the quote position
                # (this way, the closing quote isn't seen as an opening quote
                # in the second branch.)
)

这种方式使用 \G 锚点来确保所有匹配的空格都在引号之间.

This way uses the \G anchors to ensure that all matched whitespaces are between the quotes.

边缘情况:

  • 有一个孤立的开头引用:在这种情况下,从最后一个引用到字符串末尾的所有空格都被替换.但是,如果您愿意,您可以更改此行为,添加前瞻以检查结束引号是否存在:

  • there's an orphan opening quote: In this case, all whitespaces from the last quote until the end of the string are replaced. But if you want you can change this behavior adding a lookahead to check if the closing quote exists:

~(?:\G(?!\A)|"(?=[^"]*"))[^"\s]*\K(?:\s|"(*SKIP)(*F))~

双引号可以包含必须被忽略的转义双引号:您必须像这样描述转义字符:

double quotes can contain escaped double quotes that have to be ignored: You have to describe escaped characters like this:

~(?:\G(?!\A)|")[^"\s\\\\]*+(?:\\\\\S[^"\s\\\\]*)*+(?:\\\\?\K\s|"(*SKIP)(*F))~

@revo 建议的其他策略:使用前瞻检查某个位置的剩余报价数量是奇数还是偶数:

Other strategy suggested by @revo: check if the number of remaining quotes at a position is odd or even using a lookahead:

\s(?=[^"]*+(?:"[^"]*"[^"]*)*+")

这是一个短模式,但对于长字符串可能会出现问题,因为对于每个有空格的位置,您必须检查字符串直到最后一个引号和前瞻.

It is a short pattern, but it can be problematic with long strings since for each position with a whitespace you have to check the string until the last quote with the lookahead.

这篇关于php ->preg_replace ->仅删除引号之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆