【发布时间】:2023-03-13 22:56:02
【问题描述】:
如何将这些替换组合成一个正则表达式?
$style = $node->getAttribute("style");
$style = mb_ereg_replace("(direction:[[:space:]]*(rtl|ltr);)", "", $style) . " direction: {$direction};"; // remove existing direction-attribute and add the new one
$style = mb_ereg_replace("(^[[:space:]]*)|([[:space:]]*$)", "", $style); // trim spaces at the end and beginning
$style = mb_ereg_replace("([[:space:]]){2,}", " ", $style); // limit spaces to one at a time
$node->setAttribute("style", $style);
表达式按预期工作,但我想将它们组合成少于三个替换语句。
我不能只替换现有的方向属性,因为我不知道有没有。
编辑
为前两个替换添加了替换:
$style = mb_ereg_replace("(direction:[[:space:]]*(rtl|ltr);)|(^[[:space:]]*)|([[:space:]]*$)", "", $style) . " direction: {$direction};"; // remove existing direction-attribute and trim spaces at the end and beginning and add the new one
$style = mb_ereg_replace("([[:space:]]){2,}", " ", $style); // limit spaces to one at a time
【问题讨论】:
-
您可以将它们列为alternatives。
-
我知道,但我问的是mb_ereg_replace()。如果你能指出一个更好的解决方案,而不仅仅是告诉我不应该使用我没有问过的东西,我会很高兴,@Truth。
-
@Truth:
mb_ereg不是。 (同样被弃用并不意味着它可以很快被移除。) -
@mario,我知道如何将前两者结合起来作为替代方案。但是第三个呢,它不会用一个空格替换匹配项。
-
@fragmentedreality:
preg_replace()怎么样?
标签: php regex multibyte-functions