Last active
June 20, 2016 13:21
-
-
Save jepster/91114ab81b6dbbab024debdcf963afb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dom = new \DOMDocument('1.0', 'UTF-8'); | |
// PHP will output warnings about non-standard HTML. Suppress it by "@". | |
@$dom->loadHTML($source); | |
// Iterate over all link-elements. | |
foreach ($dom->getElementsByTagName('link') as $node) { | |
// Copy the element to be able to replace it. | |
$updated_element = $node; | |
$href_value = $updated_element->getAttribute( 'href' ); | |
// Checks if the value contains a standard violating character. | |
if (is_int(strpos($href_value, ']=within'))) { | |
// Encodes the URL to valid href value. | |
$href_value = drupal_urlencode($href_value); | |
$updated_element->setAttribute('href', $href_value); | |
// Replace the wrong html markup. | |
$node->parentNode->replaceChild($updated_element, $node); | |
} | |
} | |
// Get the HTML markup. | |
$html_markup_with_wrappers = $dom->saveHtml(); | |
// Remove the unnecessary wrappers. | |
$my_html_markup = preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', | |
$html_markup_with_wrappers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment