So... All properties have been sorted perfectly
BUT
how can I replace (/merge) them back on to the original $css value ?
This code is pretty much self explanatory but I have add what I'm expecting to get just in case... Small tweak needed here :)
<?php
function mySort($arr) {
$arr = explode( ";" , $arr);
sort($arr);
array_shift($arr);
$returnMe = NULL;
foreach ($arr as $key) {
$returnMe .= $key.';';
}
unset($arr);
return $returnMe."\r\n";
}
$css = <<<EOF
body {
z-index:9;
padding:0;
margin:0;
line-height:10px;
}
p {
z-index: 9;
font-size: 10px;
}
h1,h2,h3,h4,h5,h6 {
z-index: 2;
padding: 0;
margin: 0;
font-size: 100%;
border: 0 none;
}
EOF;
echo '<pre>'.
preg_replace_callback( '~.*?{(.*?)}~s',
function ($match) {
return mySort( $match[1] );
}
,$css )
.'</pre>';
I'm expecting to get :
body {
line-height:10px;
margin:0;
padding:0;
z-index:9;
}
p {
font-size: 10px;
z-index: 9;
}
h1,h2,h3,h4,h5,h6 {
border: 0 none;
font-size: 100%;
margin: 0;
padding: 0;
z-index: 2;
}
You're matching the CSS selector but aren't adding it back into the returned value - it's just being replaced by the callback function
So what you need to do is capture those parts too, and return them from the callback, like this:
echo '<pre>'.
preg_replace_callback( '~(.*?{)(.*?)(})~s', function ($match) {
return $match[1] . mySort( $match[2] ) . $match[3];
}
,$css )
.'</pre>';