-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from DivanteLtd/fix_load_html
Fix loadHTML
- Loading branch information
Showing
2 changed files
with
54 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PimcoreDevkitBundle\Service; | ||
|
||
/** | ||
* Class DomService | ||
* @package PimcoreDevkitBundle\Service | ||
*/ | ||
class DomService | ||
{ | ||
/** | ||
* @param string $html | ||
* @return \DOMDocument | ||
*/ | ||
public function loadHTML(string $html): \DOMDocument | ||
{ | ||
$doc = new \DOMDocument(); | ||
$doc->loadHTML( | ||
mb_convert_encoding("<div>$html</div>", 'HTML-ENTITIES', 'UTF-8') | ||
); | ||
|
||
$container = $doc->getElementsByTagName('div')->item(0); | ||
$container = $container->parentNode->removeChild($container); | ||
while ($doc->firstChild) { | ||
$doc->removeChild($doc->firstChild); | ||
} | ||
|
||
while ($container->firstChild ) { | ||
$doc->appendChild($container->firstChild); | ||
} | ||
|
||
return $doc; | ||
} | ||
} |
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