Removing whitespace-characters, except inside quotation marks in PHP?

纵然是瞬间 提交于 2019-12-06 04:44:29

问题


I need to remove all whitespace from string, but quotations should stay as they were.

Here's an example:

string to parse:
hola hola "pepsi cola" yay

output:
holahola"pepsi cola"yay

Any idea? I'm sure this can be done with regex, but any solution is okay.


回答1:


We could match strings or quotations with

[^\s"]+|"[^"]*"

So we just need to preg_match_all and concatenate the result.


Example:

$str = 'hola hola "pepsi cola" yay';

preg_match_all('/[^\s"]+|"[^"]*"/', $str, $matches);

echo implode('', $matches[0]);
// holahola"pepsi cola"yay



回答2:


Martti, resurrecting this question because it had a simple solution that lets you do the replace in one go—no need for implode. (Found your question while doing some research for a general question about how to exclude patterns in regex.)

Here's our simple regex:

"[^"]*"(*SKIP)(*F)|\s+

The left side of the alternation matches complete "quoted strings" then deliberately fails. The right side matches whitespace characters, and we know they are the right whitespace characters because they were not matched by the expression on the left.

This code shows how to use the regex (see the results at the bottom of the online demo):

<?php
$regex = '~"[^"]*"(*SKIP)(*F)|\s+~';
$subject = 'hola hola "pepsi cola" yay';
$replaced = preg_replace($regex,"",$subject);
echo $replaced."<br />\n";
?>

Reference

How to match (or replace) a pattern except in situations s1, s2, s3...



来源:https://stackoverflow.com/questions/3824964/removing-whitespace-characters-except-inside-quotation-marks-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!