Php 我的代码中有什么错误?(菲律宾)

Php 我的代码中有什么错误?(菲律宾),php,css,Php,Css,我写这段代码是为了替换从右到左和从左到右的css选择器,但当我运行这段代码时,不会替换任何文本,并返回源代码 <?php $headerString = 'Content-type: text/plain; charset="utf-8"'; header($headerString); $file = 'test.css'; $cssFile = file_get_contents($file); $pattern1 = '/(\.|\#).*(left|right).*[\n|

我写这段代码是为了替换从右到左和从左到右的css选择器,但当我运行这段代码时,不会替换任何文本,并返回源代码

<?php

$headerString = 'Content-type: text/plain; charset="utf-8"';

header($headerString);

$file = 'test.css';
$cssFile = file_get_contents($file);

$pattern1 = '/(\.|\#).*(left|right).*[\n| |  |\n\n|\n ]\{/';

preg_match_all($pattern1, $cssFile, $matches);

$matches = $matches[0];

$patternT = array();

$replacementT = array();

foreach ($matches as $key1 => $val1) {
    $patternT[$key1] = preg_replace(array(
            '/\./',
            '/\#/',
            '/\:/',
            '/\,/',
            '/\(/',
            '/\)/',
            '/\//',
            '/\%/',
            '/\;/',
            '/\{/',
            '/\}/',
    ), array(
            '\.',
            '\#',
            '\:',
            '\,',
            '\(',
            '\)',
            '\/',
            '\%',
            '\;',
            '\{',
            '\}',
    ), $val1);
}
foreach ($patternT as $key2 => $val2) {
    $patternT[$key2] = '/'.$val2.'/';
}

foreach ($matches as $key3 => $val3) {
    $replacementT[$key3] = preg_replace(array(
            '/right/',
            '/left/',
    ), array(
            '123456789right123456789',
            '123456789left123456789',

    ), $val3);
}
foreach ($replacementT as $key4 => $val4) {
    $replacementT[$key4] = preg_replace(array(
            '/123456789right123456789/',
            '/123456789left123456789/',
    ), array(
            'left',
            'right',

    ), $val4);
}


//Work    
echo preg_replace($patternT[1], $replacementT[1], $cssFile);

//Not work
echo preg_replace($patternT, $replacementT, $cssFile);

    ?>

所以。。。等等,您只想相互切换

最简单的方法就是执行
$out=strtr($in,array(“left”=>“right”,“right”=>“left”))

这可能会干扰其他事情-例如,如果背景图像中有
leftarrow.png
,它将更改为
rightarrow.png
。。。也许这是件好事,但如果不是

$out = preg_replace_callback("/\b(?:right|left)\b/",function($m) {
    if( $m[0] == "left") return "right";
    return "left";
},$in);

在这种情况下,请尝试使用str_replcae函数,因为您知道字符串。 我不是JS的超级粉丝,所以我建议:

 str_replace('/\./', '\.', $pattern1);
我希望这对你有帮助。
使用循环执行其余操作。

它位于数组中,您应该将其置于循环中。。