Skip to content

Instantly share code, notes, and snippets.

@ali-cedcoss
Last active October 3, 2021 07:29
Show Gist options
  • Save ali-cedcoss/ec953913173f4a1487d1f64ab57973c0 to your computer and use it in GitHub Desktop.
Save ali-cedcoss/ec953913173f4a1487d1f64ab57973c0 to your computer and use it in GitHub Desktop.
mroparts cleaning the eBay description
$doc = new DOMDocument();
$doc->loadHTML($itemDetails['Item']['Description']);
$this->ced_ebay_removeElementsByTagName('style', $doc);
$doc->saveHtml();
$finder = new DomXPath($doc);
$classname="block";
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
$tmp_dom = new DOMDocument();
foreach ($nodes as $key=>$node)
{
if(0 == $key){
$tmp_dom->appendChild($tmp_dom->importNode($node,true));
}
}
$tmp_dom->saveHTML();
$finder_new = new DomXPath($tmp_dom);
$classname_row = "row";
$nodes_row = $finder_new->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname_row ')]");
$find_images = $finder_new->query("//img");
foreach ($find_images as $image) {
// Remove that image from its parent
$image->parentNode->removeChild($image);
}
$tmp_dom_for_row = new DOMDocument();
foreach ($nodes_row as $key=>$node)
{
if($key > 0) {
$tmp_dom_for_row->appendChild($tmp_dom_for_row->importNode($node,true));
}
}
$innerHTML.=trim($tmp_dom_for_row->saveHTML());
$cleaned_product_description = $innerHTML;
$product_id = wp_insert_post(
array(
'post_title' => $itemDetails['Item']['Title'],
'post_status' => 'publish',
'post_type' => 'product',
'post_content' => $cleaned_product_description,
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment