2024-02-04 10:21:56 +01:00
|
|
|
<?php
|
|
|
|
function inlineLocalStylesFromHref($inputString) {
|
|
|
|
// Define the regular expression pattern to match <link> tags with href attribute for CSS files
|
2024-02-04 10:30:39 +01:00
|
|
|
$pattern = '/<link.*?(?:(?:rel=["\']stylesheet["\'].*?href=["\']\/(.*?)["\'])|(?:href=["\']\/(.*?)["\'].*?rel=["\']stylesheet["\'])).*?>/i';
|
2024-02-04 10:21:56 +01:00
|
|
|
|
|
|
|
// Use preg_replace_callback to replace matched link tags with inline styles
|
2024-02-04 10:24:52 +01:00
|
|
|
$outputString = preg_replace_callback($pattern, function($match) {
|
2024-02-04 10:21:56 +01:00
|
|
|
// Extract the href attribute value
|
2024-02-04 10:26:46 +01:00
|
|
|
$href = isset($match[1]) ? $match[1] : $match[2];
|
2024-02-04 10:21:56 +01:00
|
|
|
|
2024-02-04 10:28:34 +01:00
|
|
|
$fname = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
|
2024-02-04 10:33:54 +01:00
|
|
|
echo "from $fname";
|
2024-02-04 10:21:56 +01:00
|
|
|
// Get the content of the local CSS file
|
2024-02-04 10:24:52 +01:00
|
|
|
$cssContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $href);
|
2024-02-04 10:21:56 +01:00
|
|
|
|
2024-02-04 10:32:08 +01:00
|
|
|
// Replace url() declarations in the CSS content with inline data
|
2024-02-04 10:34:57 +01:00
|
|
|
$cssContent = preg_replace_callback('/url\(["\']?\/(.*?)(?:[?#].*?)?["\']?\)/i', function($urlMatch) {
|
2024-02-04 10:32:08 +01:00
|
|
|
// Extract the URL value
|
|
|
|
$url = $urlMatch[1];
|
2024-02-04 10:35:27 +01:00
|
|
|
echo "Hehe: $url";
|
2024-02-04 10:32:08 +01:00
|
|
|
|
|
|
|
// Get the content of the external file specified in the url()
|
2024-02-04 10:34:57 +01:00
|
|
|
$fileContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $url);
|
2024-02-04 10:32:08 +01:00
|
|
|
|
|
|
|
// Base64 encode the content for inline use
|
|
|
|
$base64Content = base64_encode($fileContent);
|
|
|
|
|
|
|
|
// Create the inline data URI
|
2024-02-04 10:33:54 +01:00
|
|
|
$dataUri = 'url("data:application/octet-stream;base64,' . $base64Content . '")';
|
2024-02-04 10:32:08 +01:00
|
|
|
|
|
|
|
return $dataUri;
|
|
|
|
}, $cssContent);
|
|
|
|
|
|
|
|
// Create an inline style tag with the modified CSS content
|
2024-02-04 10:21:56 +01:00
|
|
|
return "<style>\n{$cssContent}\n</style>";
|
|
|
|
}, $inputString);
|
|
|
|
|
|
|
|
return $outputString;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inlineScriptFromSrc($inputString) {
|
|
|
|
// Define the regular expression pattern to match <script> tags with src attribute
|
2024-02-04 10:24:52 +01:00
|
|
|
$pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
|
2024-02-04 10:21:56 +01:00
|
|
|
|
|
|
|
// Use preg_replace_callback to replace matched script tags with inline script
|
2024-02-04 10:24:52 +01:00
|
|
|
$outputString = preg_replace_callback($pattern, function($match) {
|
2024-02-04 10:21:56 +01:00
|
|
|
// Extract the src attribute value
|
|
|
|
$src = $match[1];
|
|
|
|
|
|
|
|
// Get the content of the JavaScript file
|
2024-02-04 10:24:52 +01:00
|
|
|
$jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
|
2024-02-04 10:21:56 +01:00
|
|
|
|
|
|
|
// Escape the JavaScript content for inline use
|
|
|
|
$escapedJsContent = htmlspecialchars($jsContent, ENT_QUOTES, 'UTF-8');
|
|
|
|
|
|
|
|
// Create an inline script with the escaped content
|
|
|
|
return "<script>\n{$escapedJsContent}\n</script>";
|
|
|
|
}, $inputString);
|
|
|
|
|
|
|
|
return $outputString;
|
|
|
|
}
|