43 lines
1.8 KiB
PHP
43 lines
1.8 KiB
PHP
<?php
|
|
function inlineLocalStylesFromHref($inputString) {
|
|
// Define the regular expression pattern to match <link> tags with href attribute for CSS files
|
|
$pattern = '/<link.*?(?:href=["\']\/(.*?)["\'].*?rel=["\']stylesheet["\']|rel=["\']stylesheet["\'].*?href=["\']\/(.*?)["\']).*?>/i';
|
|
|
|
// Use preg_replace_callback to replace matched link tags with inline styles
|
|
$outputString = preg_replace_callback($pattern, function($match) {
|
|
// Extract the href attribute value
|
|
$href = isset($match[1]) ? $match[1] : $match[2];
|
|
|
|
$fname = $_SERVER['DOCUMENT_ROOT'] . '/' . $href;
|
|
echo "from $fname";
|
|
// Get the content of the local CSS file
|
|
$cssContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $href);
|
|
|
|
// Create an inline style tag with the CSS content
|
|
return "<style>\n{$cssContent}\n</style>";
|
|
}, $inputString);
|
|
|
|
return $outputString;
|
|
}
|
|
|
|
function inlineScriptFromSrc($inputString) {
|
|
// Define the regular expression pattern to match <script> tags with src attribute
|
|
$pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
|
|
|
|
// Use preg_replace_callback to replace matched script tags with inline script
|
|
$outputString = preg_replace_callback($pattern, function($match) {
|
|
// Extract the src attribute value
|
|
$src = $match[1];
|
|
|
|
// Get the content of the JavaScript file
|
|
$jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
|
|
|
|
// 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;
|
|
} |