What are the best practices for converting relative paths to absolute paths in CSS using PHP?

When working with CSS files that contain relative paths to assets like images or fonts, it's important to convert those paths to absolute paths to ensure they are correctly resolved by the browser. One way to achieve this in PHP is by using the `dirname(__FILE__)` function to get the absolute path of the CSS file and then appending the relative path to it.

// Get the absolute path of the CSS file
$cssFilePath = dirname(__FILE__) . '/style.css';

// Define the base URL for assets
$baseUrl = 'https://www.example.com/assets/';

// Read the CSS file
$cssContent = file_get_contents($cssFilePath);

// Replace relative paths with absolute paths
$cssContent = preg_replace('/url\((?!["\']?(?:data|http|https):)["\']?([^\'")]+)["\']?\)/', 'url(' . $baseUrl . '$1)', $cssContent);

// Output the modified CSS content
echo $cssContent;