0

I have a big .css file which is all coded inline. That makes it very inelegant and hard to find the elements accurately. I want to paste it into a PHP function which gives a space after each "{" bracket, cause if I would do it manually it would take me more time.

which function should I use?

3 Answers3

1
$input_data = str_replace('}', '} ', $input_data);
Max Shaian
  • 418
  • 4
  • 11
0

You may use preg_replace here:

$input = "p.a {
        font: 15px arial, sans-serif;
    }";
$output = preg_replace("/\{/", " {", $input);
echo $input . "\n";
echo $output;

This prints:

p.a {
        font: 15px arial, sans-serif;
    }
p.a  {    <-- extra space here
        font: 15px arial, sans-serif;
    }
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

If your only interest is to add a space after all the "{", toy could easily use the following code:

$CSS = '.sample{
    padding: 10px;
}';
echo 'Old CSS code: '.$CSS.PHP_EOL.PHP_EOL;

$cleanCSS = str_replace('{', '{ ', $css);
echo 'Clean CSS code: '.$cleanCSS;

This code prints:

Old CSS code: .sample{
    padding: 10px;
}

Clean CSS code: .sample {
    padding: 10px;
}