用PHP中的preg_replace删除替换空间?

<a param1="false" class="param2" id="id#12" href="#1">toto</a>

I have made a preg_replace in PHP to remove certain params in a html text:

$re = "/(param1=\"false\"|class=\"param2\"|id=\"id#([^\"]+)\")/";

$text = preg_replace($re, " ", $data->text->value); 

I obtain this html link:

<a href="#1">toto</a>

I would like to leave only one space beetween the and the href, is it possible to do that directly in the regex ?

Change your regular regular in the following way,

$re = '/(param1="false"\s+|class="param2"\s+|id="id#([^\"]+)"\s+)/';

You need to pass \s+ in each of your component.

And change your preg_replace() function in the following way,

$text = preg_replace($re, "", $data->text->value);

Don't replace them with space, that's it.

Assuming you have to use all the backrefrences in your "preg_match". This is the only way you can accomplish

$re = "/<a.*(param1=\"false\"|class=\"param2\"|id=\"id#([^\"]+)\")/";
$text = preg_replace($re, "<a", $data->text->value);

You could try this:

$re = "/(param1=\"false\"|class=\"param2\"|id=\"id\#([^\"]+)\")([\ ]{1,}?)/";
$text = preg_replace($re, "", $data->text->value);

Regex101