使用PHPQuery选择带有查询字符串的URL

时间:2015-04-11 15:15:37

标签: php phpquery

我想根据是否有一个URL属性设置为特定值的子节点来选择XML节点。

我认为这段代码显示phpQuery没有正确解析URL ...但我可能错过了关于转义= ...

的内容

有什么想法吗?

<?php
include '../libs/phpQuery.php';
phpQuery::newDocumentXML('
   <doc><item>item 1<link url="http://example.com" /></item>
        <item>item 2<link url="http://example.com?abc" /></item>
        <item>item 3<link url="http://example.com?abc=" /></item>
        <item>item 4<link url="http://example.com?abc=21" /></item>
   </doc>');

echo "<pre>
".
pq("link[url='http://example.com']:first")->parents('item')->xml()
."
".
pq("link[url='http://example.com?abc']:first")->parents('item')->xml()
."
".
pq("link[url='http://example.com?abc=']:first")->parents('item')->xml()
."
".
pq("link[url='http://example.com?abc=21']:first")->parents('item')->xml()
."
</pre>";
?>

这是返回

<pre>
item 1<link url="http://example.com"/>
item 2<link url="http://example.com?abc"/>
item 2<link url="http://example.com?abc"/>
item 2<link url="http://example.com?abc"/>
</pre>

1 个答案:

答案 0 :(得分:0)

在第749行的phpQuery / phpQueryObject.html中

使用

解析属性及其所需值
                    $value = null;
                    list($attr, $value) = explode('=', $attr);
                    $value = trim($value, "'\"");

所以第二个&#39; =&#39;之后的一切都会丢失(爆炸成第三个没有被捕获的值)。我改成了

                // attr with specifed value
                if (mb_strpos($s, '=')) {
                    $value = null;
                    $raw = $attr;
                    parse_str($raw,$ary);
                    $attr = current(array_keys($ary));
                    $value = substr($raw,strlen($attr)+1);
                    $value = trim($value, "'\"");

它有点(非常)naff,但它适用于我的应用程序!

请注意,密钥是从“解析”中提取的。 value,则该值是字符串的剩余部分。

相关问题