我寫這段代碼來替換從右到左和從左到右的css選擇器,但是當我運行這個時,並沒有替換任何文本,並返回源代碼。我的代碼中有什麼錯誤? (php)
<?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);
?>
它是在數組中,你應該把它循環.. –