Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jepster
Last active June 20, 2016 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepster/91114ab81b6dbbab024debdcf963afb0 to your computer and use it in GitHub Desktop.
Save jepster/91114ab81b6dbbab024debdcf963afb0 to your computer and use it in GitHub Desktop.
<?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