【问题标题】:Concatenating an xPath query in PHP在 PHP 中连接 xPath 查询
【发布时间】:2015-11-26 14:37:52
【问题描述】:

我正在使用 xPath 来查询 HTML 文档。我正在设置一个字符串作为 xPath 查询的内容。所以在这种情况下,'$category_titles' 被设置为查询返回的文本。

$category_titles = $xpath->query('//*[@id="test"]/div/div/h1');

有没有办法将两个 xpath 查询的值传递给我的字符串?如此有效,我需要将 $category_titles 设置为两个单独的 xPath 值的结果。

【问题讨论】:

  • 你能举个例子吗,你希望最后会有什么?这有点难以理解
  • 将它传递给 2 个临时变量并连接它们??进入你的 $category_titles??

标签: php xpath


【解决方案1】:

你应该使用运算符 |将多条不同的路径合并为一条。 这是有效的:

$titles = $xpath->query('//*[@id="test"]/div/div/h1 | //*[@id="test1"]/div/div/h1');

【讨论】:

    【解决方案2】:

    我不知道我是否完全理解您想要什么,但如果您试图在同一个变量中获得两个 xpath 结果,唯一的方法是使用array

    $category_title = array();
    $category_titles[0] = $xpath->query('//*[@id="test"]/div/div/h1');
    $category_titles[1] = $xpath->query('//*[@id="test1"]/div/div/h1');//I changed the ID, but you can change your entire query
    

    然后,如果你想连接它,只需一个$concat = "$category_titles[0]" . "$category_titles[1]" 就足够了。

    但是,如果您尝试使用第一个查询的结果创建第二个查询然后连接,您可以这样做:

    $category_titles = $xpath->query('//*[@id="test"]/div/div/h1');
    $category_titles = "$category_titles" . $xpath->query("//*[@id=\"$category_titles\"]/div/div/h1");// Note the escaped slashes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2012-08-29
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多