Pretty much self explanatory what I'm trying to achieve here...
I thing that the function is getting $1 instead of the real actual string.
a tiny small tweak can make it work :)
Why it's not working ?
<?php
function mySort($arr) {
sort($arr);
return $arr;
}
$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( '~.*?{(.*?)}~s', implode ( ";", mySort( explode( ";" , "$1" ) ) ) , $css ).'</pre>';
You're looking for preg_replace_callback
:
echo '<pre>'.preg_replace_callback( '~.*?{(.*?)}~s',
function ($match) use ($css) {
return implode ( ";", mySort( explode( ";" , $match[1] ) ) );
},
$css ).'</pre>';
The third argument needs to be a string:
preg_replace($pattern, $replacement, $string);
Also... It is not clear in yuor code what "$1"
represents? Is this a string? the tring you want to replace? Or is it a php variable? $1
It looks as thou you are trying to pass the CSSS to mySort()
? IS that correct?