这是我的代码:
$css = ".block{float:right; text-align:right;padding:0 20px 0 0; margin:0 0 0 0;}.block h2{font-size:24px; color:#2b7538; font-weight:normal; text-align:right; padding:10px; margin:10px 0 0 0; text-align:left}.block p{line-height:30px;margin-top:20px; float:left}";
$search = array("float:left","text-align:left","float:right","text-align:right");
$replace = array("float:right","text-align:right","float:left","text-align:left");
echo str_replace($search, $replace, $css);
如何在同一时间内从左到右和从右到左替换?
答案 0 :(得分:0)
您需要暂时使用其他值。在这个例子中,我使用了“xyz”,使用了一些更明显的东西。
这里有一些代码,在一个易于重复的循环中应该(我还没有测试过)将左右值互相替换。
$css = ".block{float:right; text-align:right;padding:0 20px 0 0; margin:0 0 0 0;}.block h2{font-size:24px; color:#2b7538; font-weight:normal; text-align:right; padding:10px; margin:10px 0 0 0; text-align:left}.block p{line-height:30px;margin-top:20px; float:left}";
$from_to = array(
'left' => 'xyz',
'right' => 'left',
'xyz' => 'right'
);
foreach ($from_to as $from => $to)
{
$search = array("float:" . $from, "text-align:" . $from);
$replace = array("float:" . $to, "text-align:" . $to);
$css = str_replace($search, $replace, $css);
}
echo $css;
答案 1 :(得分:0)
$search = array('right', 'left', 'xxx');
$replace = array('xxx', 'right', 'left');
$subject = 'right then left then right';
$return = str_replace($search, $replace, $subject);
返回:
$ subject:右,然后左,然后
$ return:向左然后向右然后离开