/** * Gets a printable message. * * This function provides a method to get printable messages. * * @return string */ public function getPrintableMessage() { $ret = $this->message; if ($this->_parser != null) { $string = rtrim($this->_parser->getString()); $offset = $this->_parser->getOffset(); $rightStr = substr($string, $offset); $offset0 = $offset + strlen($rightStr) - strlen(ltrim($rightStr)); $str1 = substr($string, 0, $offset0); $offset1 = strrpos($str1, "\n"); if ($offset1 !== false) { $offset1++; } $str2 = substr($string, $offset1); $offset2 = strpos($str2, "\n"); if ($offset2 === false) { $offset2 = strlen($str2); } $str3 = substr($str2, 0, $offset2); $line = $offset0 > 0 ? substr_count($string, "\n", 0, $offset0) : 0; $column = $offset0 - $offset1; $ret = "{$str3}\n" . str_repeat(" ", $column) . "^" . $this->message; if ($line > 0) { $ret .= " (line " . ($line + 1) . ")"; } } return $ret; }
/** * Parses a string. * * This function parses a string and throws an exception if unsuccessful. * * For example: * ```php * // parses a string * $p = new MyCustomParser($string); * try { * $info = $p->parse(); * } catch(TextParserException $e) { * echo $e->getPrintableMessage(); * } * * if (!$info) { * echo "This is not a valid expressión"; * } else { * print_r($info); * } * ``` * * @param string $string String target (default is "") * * @throws Exception * @return mixed */ public function parse($string = "") { if (func_num_args() > 0) { $this->string = $string; $this->offset = 0; } $ungreedy = TextParser::UNGREEDY & $this->_flags; $ret = $this->evaluate(); if ($ret) { if ($this->_target instanceof TextParser) { $this->_target->setOffset($this->offset); } elseif (!$ungreedy && !$this->end()) { throw new TextParserException("Unrecognized expression", $this); } } return $ret; }
/** * Constructor. * * The target can be either a document (DOMDocument), a node (DOMNode), a * filename, a URL or a string. * * When the $target parameter is a URL or a filename, you can also specify * the mimetype and the charset. If this info is not provided, they will be * automatically detected. For example: * ```php * // charset and mime-type are provided * $selector = new CssParser( * 'http://www.my-site.com/document.xml', 'UTF-8', 'text/xml' * ); * * // charset and mime-type will be automatically detected * $selector = new CssParser('http://www.my-site.com/document.xml'); * ``` * * Example 1: The target is a document * ```php * $doc = new DOMDocument("1.0", "UTF-8"); * $doc->loadXML( * '<root><item id="101" /><item id="102" /></root>' * ); * $selector = new CssParser($doc); * ``` * * Example 2: The targt is a node * ```php * $doc = new DOMDocument("1.0", "UTF-8"); * $doc->loadXML( * '<root><item id="101" /><item id="102" /></root>' * ); * $root = $doc->documentElement; * $selector = new CssParser($root); * ``` * * Example 3: The target is a filename * ```php * $selector = new CssParser('/path/to/my/document.xml'); * ``` * * Example 4: The target is an URL * ```php * $selector = new CssParser('http://www.my-site.com/document.xml'); * ``` * * @param DOMDocument|DOMNode|string $target Target object * @param string $charset Charset (default is "") * @param string $mimetype Mime-type (default is "") * * @return void */ public function __construct($target, $charset = "", $mimetype = "") { $this->_pseudoFilters = array(); $this->_combinators = array(); if ($target instanceof DOMDocument) { $this->_node = $target->documentElement; } elseif ($target instanceof DOMNode) { $this->_node = $target; } elseif (is_string($target)) { $this->_load($target, $charset, $mimetype); } // registers pseudo filters $this->registerPseudoFilter("first", "CssParserFilterPseudoFirst"); $this->registerPseudoFilter("last", "CssParserFilterPseudoLast"); $this->registerPseudoFilter("eq", "CssParserFilterPseudoEq"); $this->registerPseudoFilter("nth", "CssParserFilterPseudoEq"); $this->registerPseudoFilter("even", "CssParserFilterPseudoEven"); $this->registerPseudoFilter("odd", "CssParserFilterPseudoOdd"); $this->registerPseudoFilter("lt", "CssParserFilterPseudoLt"); $this->registerPseudoFilter("gt", "CssParserFilterPseudoGt"); $this->registerPseudoFilter("nth-child", "CssParserFilterPseudoNthChild"); $this->registerPseudoFilter("not", "CssParserFilterPseudoNot", "selectorList"); $this->registerPseudoFilter("first-child", "CssParserFilterPseudoFirstChild"); // registers combinators $this->registerCombinator("", "CssParserCombinatorDescendant"); $this->registerCombinator(">", "CssParserCombinatorChild"); $this->registerCombinator("+", "CssParserCombinatorAdjacent"); $this->registerCombinator("~", "CssParserCombinatorGeneral"); parent::__construct(""); }