test inlining

This commit is contained in:
Bruno Rybársky 2024-02-04 10:24:52 +01:00
parent f67c6a6186
commit 20d8f2513c

@ -1,17 +1,15 @@
<?php <?php
function inlineLocalStylesFromHref($inputString) { function inlineLocalStylesFromHref($inputString) {
$basePath = '/path/to/your/local/files'; // Hardcoded base path
// Define the regular expression pattern to match <link> tags with href attribute for CSS files // Define the regular expression pattern to match <link> tags with href attribute for CSS files
$pattern = '/<link.*?href=["\']\/assets\/(.*?)["\'].*?rel=["\']stylesheet["\'].*?>/i'; $pattern = '/<link.*?href=["\']\/(.*?)["\'].*?rel=["\']stylesheet["\'].*?>/i';
// Use preg_replace_callback to replace matched link tags with inline styles // Use preg_replace_callback to replace matched link tags with inline styles
$outputString = preg_replace_callback($pattern, function($match) use ($basePath) { $outputString = preg_replace_callback($pattern, function($match) {
// Extract the href attribute value // Extract the href attribute value
$href = $match[1]; $href = $match[1];
// Get the content of the local CSS file // Get the content of the local CSS file
$cssContent = file_get_contents($basePath . '/assets/' . $href); $cssContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $href);
// Create an inline style tag with the CSS content // Create an inline style tag with the CSS content
return "<style>\n{$cssContent}\n</style>"; return "<style>\n{$cssContent}\n</style>";
@ -21,18 +19,16 @@ function inlineLocalStylesFromHref($inputString) {
} }
function inlineScriptFromSrc($inputString) { function inlineScriptFromSrc($inputString) {
$basePath = '/path/to/your/local/files'; // Hardcoded base path
// Define the regular expression pattern to match <script> tags with src attribute // Define the regular expression pattern to match <script> tags with src attribute
$pattern = '/<script.*?src=["\']\/assets\/(.*?)["\'].*?>\s*<\/script>/i'; $pattern = '/<script.*?src=["\']\/(.*?)["\'].*?>\s*<\/script>/i';
// Use preg_replace_callback to replace matched script tags with inline script // Use preg_replace_callback to replace matched script tags with inline script
$outputString = preg_replace_callback($pattern, function($match) use ($basePath) { $outputString = preg_replace_callback($pattern, function($match) {
// Extract the src attribute value // Extract the src attribute value
$src = $match[1]; $src = $match[1];
// Get the content of the JavaScript file // Get the content of the JavaScript file
$jsContent = file_get_contents($basePath . '/assets/' . $src); $jsContent = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/' . $src);
// Escape the JavaScript content for inline use // Escape the JavaScript content for inline use
$escapedJsContent = htmlspecialchars($jsContent, ENT_QUOTES, 'UTF-8'); $escapedJsContent = htmlspecialchars($jsContent, ENT_QUOTES, 'UTF-8');