Problem bei preg_replace() mit Arrays

mtronics

Mitglied
Hi,
ich arbeite gerade an einem Stylesheet-Designer. Man gibt die gewünschten Farben der einzelnen HTML-Elemente in ein Formular ein und per preg_replace() soll dann das Template abgeändert werden. Um mir einige Tipparbeit zu ersparen arbeite ich mit Arrays:

PHP:
$search = Array(
            '/body{.*background-color:(.*);.*}/',
            '/\#content{.*background-color:(.*);.*}/',
            '/color:(.*);/',
            '/border-color:(.*);/',
            '/:link{.*color:(.*);.*}/',
            '/:hover{.*background-color:(.*);.*}/',
            '/:active{.*color:(.*);.*}/',
            '/\.smalltext{.*color:(.*);.*}/'
            );
$replace = Array(
            $data['color_background'],
            $data['color_page'],
            $data['color_text'],
            $data['color_border'],
            $data['color_link'],
            $data['color_link'],
            $data['color_link_active'],
            $data['color_text_small']
            );
$stylesheet = fopen($page['path'] . 'style/style.css', 'r+');
$theme = preg_replace($search, $replace, $stylesheet);
fclose($stylesheet);

Hier könnte man fopen() auch durch einen String ersetzen, jedoch lese ich das Template aus einem anderen Ordner aus.

Mein Stylesheet:

Code:
body{    background-color: #101010;
    padding: 0px;
}


h1{
    font-size: 200%;
    font-family: Arial, Tahoma;
    color: #ffffff;
    margin: 0 auto;
}


h2{
    font-size: 150%;
    font-family: Arial, Tahoma;
    color: #cdcdcd;
    margin: 0 auto;
}


h3{
    font-size: 100%;
    font-family: Arial, Tahoma;
    color: #ffffff;
    margin: 0 auto;
    text-align: center;
}


h4{
    font-size: 150%;
    font-family: Arial, Tahoma;
    color: #ffffff;
    margin: 0 auto;
    margin-right: 7px;
    float: left;
}


h5{
    font-size: 110%;
    font-family: Arial, Tahoma;
    color: #ffffff;
    margin: 0 auto;
    text-decoration: underline;
    margin-bottom: 7px;
}


p{
    font-size: 100%;
    color: #ffffff;
    font-family: Arial, Tahoma;
    margin: 0px;
}


li{
    font-size: 100%;
    color: #ffffff;
    font-family: Arial, Tahoma;
    margin: 0px;
}


input{
    margin: 0px;
    margin-top: -1px;
}


textarea{
    margin: 0px;
    margin-top: -1px;
}


td{
    font-size: 100%;
    color: #ffffff;
    font-family: Arial, Tahoma;
    margin: 0px;
}


hr{
    height: 1px;
    background-color: #cdcdcd;
    border: 0px;
    margin-left: -7px;
    margin-right: -7px;
}


a{
    font-size: 100%;
    font-family: Arial, Tahoma;
    transition: color 1s, text-decoration 1s;
    -moz-transition: color 1s, text-decoration 1s;
    -webkit-transition: color 1s, text-decoration 1s;
    margin: 0px;
}


a:link{
    color: #ffffff;
    text-decoration: underline;
}


a:hover{
    color: #1659de;
    text-decoration: none;
}


a:visited{
    color: #cdcdcd;
    text-decoration: underline;
}


a:active{
    color: #1457dc;
    text-decoration: underline;
}


#navbar a{
    font-size: 100%;
    text-decoration: none;
}


#navbar{
    width: 100%;
    height: 30px;
    display: block;
    position: fixed;
    top: 0px;
    left: 0px;
    background-color: #303030;
    padding: 4px;
    margin: 0;
    overflow-y: hidden;
    border-bottom: #cdcdcd 1px solid;
    z-index: 100;
    box-shadow: 0px 3px 5px #101010;
}


#content{
    width: 90%;
    background-color: #303030;
    position: relative;
    top: 52px;
    padding: 7px;
    margin: 0 auto;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}


#tilecontainer{
    width: 100%;
    margin: 0px;
    position: absolute;
    top: 52px;
    left: 0px;
    padding: 0px;
}


.smallimage{
    margin: 3px;
    float: left;
    padding: 0px;
    border: #cdcdcd solid 1px;
    width: calc(20% - 8px);
    width: -moz-calc(20% - 8px);
    width: -webkit-calc(20% - 8px);
}


.smalltext{
    font-size: 80%;
    font-family: Arial, Tahoma;
    color: #b6b6b6;
    text-align: left;
}


.navbarimage{
    width: 30px;
    height: 30px;
    padding: 0px;
    margin-right: 7px;
    float: left;
}


.tileimage{
    width: 62%;
    margin-left: 19%;
    margin-right: 19%;
    margin-top: 19%;
    margin-bottom: 3%;
}


.fatborder{
    border:#ffffff solid 5px;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    margin: 3px;
}


.infobox{
    width: 100%;
    height: 20px;
    border: 1px solid;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    margin: 0 auto;
    margin-left: -4px;
    margin-bottom: 7px;
    padding: 3px;
}


.listbox{
    width: 100%;
    height: 20px;
    border: 1px solid #cdcdcd;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    margin: 0 auto;
    margin-left: -4px;
    margin-bottom: 7px;
    padding: 3px;
    background-color: #ffffff;
}


.listimage{
    width: 14px;
    height: 14px;
    padding: 0px;
    margin-right: 7px;
    float: left;
}


.listbox p{
    color: #101010;
}


.tabcontainer{
    width: 100%;
    height: 30px;
    padding: 0px;
    margin: 7px;
    margin-bottom: -8px;
    margin-left: -7px;
    margin-right: -7px;
    z-index: 80;
}


.tab{
    width: 10%;
    height: 30px;
    float: left;
    margin: 0 auto;
    margin-left: 0.2%;
    margin-right: 0.2%;
    padding: 0px;
    border: 1px solid #cdcdcd;
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    -webkit-border-top-left-radius: 3px;
    -webkit-border-top-right-radius: 3px;
    -moz-border-topleft-radius: 3px;
    -moz-border-topright-radius: 3px;
}


.tab p{
    margin:0 auto;
    text-align: center;
    margin-top: 5px;
}


.activetab p{
    color: #303030;
}


.activetab{
    background-color: #ffffff;
    border-bottom: 0px;
}


.inactivetab{
    background-color: #303030;
    border-bottom: 1px;
}


.tile{
    width: 14%;
    margin-left: 1%;
    margin-bottom: 1%;
    float: left;
    padding: 0px;
    background-color: #303030;
    transition: background-color 1s;
    -moz-transition: background-color 1s;
    -webkit-transition: background-color 1s;
}


.tile:hover{
    background-color: #1659de;
}


.tile:active{
    background-color: #1457dc;
}

Mein Problem: Die Variable $theme beinhaltet nicht wie erwartet das abgeänderte Stylesheet-Template, sondern lediglich die Wörter "Resource id #7".
Was hat das zu bedeuten und wo habe ich einen Fehler gemacht?

Ich hoffe auf baldige Hilfe.
MfG,
mtronics
 
Das ist auch ganz richtig so, denn letztendlich gibt dir fopen() einen Datei-Resourcen-Handler zurück und keine Zeichenkette. Das, was du möchtest, müsste so aussehen:
PHP:
$search = array(
  '/body{.*background-color:(.*);.*}/',
  '/\#content{.*background-color:(.*);.*}/',
  '/color:(.*);/',
  '/border-color:(.*);/',
  '/:link{.*color:(.*);.*}/',
  '/:hover{.*background-color:(.*);.*}/',
  '/:active{.*color:(.*);.*}/',
  '/\.smalltext{.*color:(.*);.*}/'
);

$replace = array(
  $data['color_background'],
  $data['color_page'],
  $data['color_text'],
  $data['color_border'],
  $data['color_link'],
  $data['color_link'],
  $data['color_link_active'],
  $data['color_text_small']
);

$stylesheet = file_get_contents($page['path'] . 'style/style.css');
$stylesheet = preg_replace($search, $replace, $stylesheet);
file_put_contents($page['path'] . 'style/style.css', $stylesheet);
 
Danke für die schnelle Antwort, jetzt funktioniert es. Ich hatte noch einen Fehler im 2. Array, aber den habe ich behoben.
 
Jetzt ist ein neues Problem aufgetreten:

color: farbe; wird jetzt komplett durch farbe ersetzt (Ich kenne mich mit preg_replace() nicht am allerbesten aus.) Wie geht das mit (.*) und dem Ersetzen mit einem Array? Und wie verhindere ich, dass bei background-color: farbe; auch color: farbe; ersetzt wird?
 
PHP:
$search = array(
  '/body{.*background-color:(.*);.*}/',
  '/\#content{.*background-color:(.*);.*}/',
  '/[^-]color:(.*);/',
  '/border-color:(.*);/',
  '/:link{[^-]*color:(.*);.*}/',
  '/:hover{.*background-color:(.*);.*}/',
  '/:active{[^-]*color:(.*);.*}/',
  '/\.smalltext{[^-]*color:(.*);.*}/'
);
 
$replace = array(
  $data['color_background'],
  $data['color_page'],
  $data['color_text'],
  $data['color_border'],
  $data['color_link'],
  $data['color_link'],
  $data['color_link_active'],
  $data['color_text_small']
);

$search = array_map(function ($item) {
  list($before, $after) = explode('(.*)', str_replace('/', '', $item), 2);
  return '/(' . $before . ')(.*)(' . $after . ')/';
}, $search);

$replace = array_map(function ($item) {
  return '$1' . $item . '$3';
}, $replace);

$stylesheet = file_get_contents($page['path'] . 'style/style.css');
$stylesheet = preg_replace($search, $replace, $stylesheet);
file_put_contents($page['path'] . 'style/style.css', $stylesheet);
 
Zuletzt bearbeitet:
Ja, ist ja klar. Deine Pattern matchen eben "Eigenschaft : Wert" und nicht nur "Wert".
Eine mögliche Lösung:

PHP:
$string = 'body{    background-color: #101010;
    padding: 0px;
}';
$pattern = '/(body\{.*background-color: )(.*)(;.*\})/sU';
$replace = '$1#123456$3';

$return = preg_replace($pattern, $replace, $string);

echo $return;

Achja, vllt ne Alternative für dich: http://lesscss.org/

edit// da war ih ja vieeeel zu langsam :D
Problem war aber auch, dass . auf meinem System keine Zeilenumbrüche matcht. Wenn du das also auch auf anderen Systemen zum laufen kriegen willst, musst du den s-Modifier noch setzen. Außerdem musste ich für den "hinteren" Teil noch nen U-Modifier benutzen.
 
Zuletzt bearbeitet:
array_map() funktioniert wie folgt: es übergibt jeden Wert eines Arrays an eine Callback-Funktion, welche diesen Wert dann manipuliert. Im ersten Fall kapselt er alles vor und nach der gewünschten Suchstelle ich runde Klammern und im zweiten Fall wird davor eine $1 und danach eine $3 gesetzt.
 
Zuletzt bearbeitet:
Zurück