Skip to content

Instantly share code, notes, and snippets.

@tieutantan
Last active July 3, 2018 03:06
Show Gist options
  • Save tieutantan/b82c9010bc393512c0b67d4a75e9769a to your computer and use it in GitHub Desktop.
Save tieutantan/b82c9010bc393512c0b67d4a75e9769a to your computer and use it in GitHub Desktop.
Function get content for package "paquettg/php-html-parser"
<?php
/* https://github.com/paquettg/php-html-parser */
public static function get_content(string $html_identity, string $needed_attribute, $source_html)
{
$dom = new \PHPHtmlParser\Dom;
$dom->load($source_html);
$array_content = [];
foreach ($dom->find($html_identity) as $content)
{
if ($needed_attribute == 'text') {
$array_content[] = strip_tags($content->outerHtml);
} elseif ($needed_attribute == 'html') {
$array_content[] = $content->outerHtml;
} else {
// get content of Attribute name
$array_content[] = $content->getAttribute($needed_attribute);
}
}
return (empty($array_content)) ? false : array_unique($array_content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment