php - 我的代码有什么错误? (php)

标签 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| |  |\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);

关于php - 我的代码有什么错误? (php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22248743/

相关文章:

php - 使用 Flex 和 PHP 上传图片

php - 解析错误: syntax error, unexpected ',' not seeing the issue [duplicate]

php - 使用 Facebook api 更新 mysql

css - 如何让我的 Google Map V3 保留圆 Angular ?

css - 根据用户选择动态更改网页中的字体/字体

javascript - 使用 jQuery 过滤下拉菜单

PHP:使用bcrypt进行密码加密

php - 无法获得简单的 PHP 表单来添加/更新到 MySQL 表

css - 如何制作:hover more mobile friendly and accessible?

css - 能够仅在 CSS 中回滚菜单