1. <?php
  2. /**
  3. * Resolves dependencies for HTML documents.
  4. *
  5. * This script aims to resolve external dependencies (CSS, JS) linked in an HTML file.
  6. * It provides a minimal solution with no external dependencies beyond core PHP.
  7. *
  8. * @param string $html_content The HTML content as a string.
  9. * @return string The HTML content with dependencies resolved (CSS and JS links updated).
  10. */
  11. function resolveDependencies(string $html_content): string
  12. {
  13. // Regex to find <link> and <script> tags.
  14. $css_regex = '/<link[^>]*rel="stylesheet"[^>]*href="([^"]*)"[^>]*>/i';
  15. $js_regex = '/<script[^>]*src="([^"]*)"[^>]*>/i';
  16. // Resolve CSS dependencies.
  17. $html_content = preg_replace_callback($css_regex, function ($matches) use ($html_content) {
  18. $href = $matches[1];
  19. // Resolve relative URLs.
  20. if (strpos($href, '/') === 0) {
  21. //Absolute URL, no changes needed.
  22. return $matches[0];
  23. } elseif (strpos($href, '.') === 0) {
  24. //Relative to current directory
  25. $href = realpath($href);
  26. if($href === false){
  27. return $matches[0]; //Keep original if realpath fails
  28. }
  29. $href = str_replace($_SERVER['DOCUMENT_ROOT'], '', $href); //remove document root
  30. if(strpos($href, '/') === 0){
  31. $href = $_SERVER['DOCUMENT_ROOT'] . $href;
  32. }
  33. } else {
  34. // Relative to parent directory
  35. $path = pathinfo($href, PATHINFO_DIRNAME);
  36. $href = $path . '/' . $href;
  37. }
  38. //Check if the resolved path is valid
  39. if (strpos($href, '.') === 0) {
  40. return $matches[0]; //keep original if invalid path
  41. }
  42. return str_replace($href, $_SERVER['DOCUMENT_ROOT'] . $href, $matches[0]); // Replace with absolute path.
  43. }, $html_content);
  44. // Resolve JavaScript dependencies.
  45. $html_content = preg_replace_callback($js_regex, function ($matches) use ($html_content) {
  46. $src = $matches[1];
  47. // Resolve relative URLs.
  48. if (strpos($src, '/') === 0) {
  49. //Absolute URL, no changes needed.
  50. return $matches[0];
  51. } elseif (strpos($src, '.') === 0) {
  52. //Relative to current directory
  53. $src = realpath($src);
  54. if($src === false){
  55. return $matches[0]; //Keep original if realpath fails
  56. }
  57. $src = str_replace($_SERVER['DOCUMENT_ROOT'], '', $src); //remove document root
  58. if(strpos($src, '/') === 0){
  59. $src = $_SERVER['DOCUMENT_ROOT'] . $src;
  60. }
  61. } else {
  62. // Relative to parent directory
  63. $path = pathinfo($src, PATHINFO_DIRNAME);
  64. $src = $path . '/' . $src;
  65. }
  66. //Check if the resolved path is valid
  67. if (strpos($src, '.') === 0) {
  68. return $matches[0]; //keep original if invalid path
  69. }
  70. return str_replace($src, $_SERVER['DOCUMENT_ROOT'] . $src, $matches[0]); // Replace with absolute path.
  71. }, $html_content);
  72. return $html_content;
  73. }
  74. //Example Usage (replace with your HTML content)
  75. $html = '<html>
  76. <head>
  77. <link rel="stylesheet" href="style.css">
  78. <link rel="stylesheet" href../styles/main.css">
  79. </head>
  80. <body>
  81. <script src="script.js"></script>
  82. <script src="js/utils.js"></script>
  83. </body>
  84. </html>';
  85. $resolved_html = resolveDependencies($html);
  86. echo $resolved_html;
  87. ?>

Add your comment