用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 ?
&lt; a param1 =“false”class =“param2”id =“id#12”href =“#1”&gt; toto&lt; / a&gt;
code> pre>
我在 PHP strong>中创建了 preg_replace strong> 删除html文本中的某些参数: p>
$ re =“/(param1 = \”false \“| class = \”param2 \“| id = \”id #([^ \“] +)\”)/“;
$ text = preg_replace($ re,”“,$ data-&gt; text-&gt; value);
code> pre>
我获得了 html strong>链接: p>
&lt; a href =“#1”&gt; toto&lt; / a&gt; code> p>
我想在和href strong>之间只留下一个空格,是否可以直接在正则表达式中执行此操作?
div>
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);