parseString( $content, array( 'output-xhtml' => true, 'numeric-entities' => true, ), 'utf8' ); $tidy->cleanRepair(); $tidy = str_replace('xmlns="http://www.w3.org/1999/xhtml"','',$tidy); $tidy = str_replace(' ','',$tidy); } elseif(@shell_exec('which tidy')) { // using tiny through cli $CLI_content = escapeshellarg($content); $tidy = `echo $CLI_content | tidy -n -q -utf8 -asxhtml 2> /dev/null`; $tidy = str_replace('xmlns="http://www.w3.org/1999/xhtml"','',$tidy); $tidy = str_replace(' ','',$tidy); } else { // no tidy library found, hence no sanitizing $tidy = $content; } $this->simpleXML = @simplexml_load_string($tidy, 'SimpleXMLElement', LIBXML_NOWARNING); if(!$this->simpleXML) { throw new Exception('CSSContentParser::__construct(): Could not parse content. Please check the PHP extension tidy is installed.'); } parent::__construct(); } /** * Returns a number of SimpleXML elements that match the given CSS selector. * Currently the selector engine only supports querying by tag, id, and class. * See {@link getByXpath()} for a more direct selector syntax. * * @param String $selector * @return SimpleXMLElement */ function getBySelector($selector) { $xpath = $this->selector2xpath($selector); return $this->getByXpath($xpath); } /** * Allows querying the content through XPATH selectors. * * @param String $xpath SimpleXML compatible XPATH statement * @return SimpleXMLElement|false */ function getByXpath($xpath) { return $this->simpleXML->xpath($xpath); } /** * Converts a CSS selector into an equivalent xpath expression. * Currently the selector engine only supports querying by tag, id, and class. * * @param String $selector See {@link getBySelector()} * @return String XPath expression */ function selector2xpath($selector) { $parts = preg_split('/\\s+/', $selector); $xpath = ""; foreach($parts as $part) { if(preg_match('/^([A-Za-z][A-Za-z0-9]*)/', $part, $matches)) { $xpath .= "//$matches[1]"; } else { $xpath .= "//*"; } $xfilters = array(); if(preg_match('/#([^#.\[]+)/', $part, $matches)) { $xfilters[] = "@id='$matches[1]'"; } if(preg_match('/\.([^#.\[]+)/', $part, $matches)) { $xfilters[] = "contains(@class,'$matches[1]')"; } if($xfilters) $xpath .= '[' . implode(',', $xfilters) . ']'; } return $xpath; } }