【问题标题】:how to find urls under double quote如何在双引号下查找网址
【发布时间】:2018-02-26 14:40:04
【问题描述】:

假设我们加载了这个问题的源代码,我们想在“childUrl”旁边找到url

或转到本站源代码并搜索“childUrl”。

<?php
$sites_html = file_get_contents("https://stackoverflow.com/questions/46272862/how-to-find-urls-under-double-quote");
$html = new DOMDocument();
@$html->loadHTML($sites_html);
foreach() {
# now i want here to echo the link alongside "childUrl"
}
?>

【问题讨论】:

    标签: php regex parsing url preg-match


    【解决方案1】:

    试试这个

    <?php
        function extract($url){
        $sites_html = file_get_contents("$url");
        $html = new DOMDocument();
        $$html->loadHTML($sites_html);
    
          foreach ($html->loadHTML($sites_html) as $row)
          {
           if($row=="wanted_url") 
           {
             echo $row;
           }
         }
     }
    ?>
    

    【讨论】:

    • 我收到此错误“可捕获的致命错误:DOMDocument 类的对象无法转换为字符串”
    • loadHTML($sites_html); foreach ($html->loadHTML($sites_html) as $row) { if((string)$row=="wanted_url") { echo $row; } } } ?>
    【解决方案2】:

    您可以使用正则表达式: 试试这段代码

    $matches = [[],[]];
    preg_match_all('/\"wanted_url\": \"([^\"]*?)\"/', $sites_html, $matches);
    foreach($matches[1] as $match) {
        echo $match;
    }
    

    这将打印所有带有wanted_url标签的url

    【讨论】:

    • 解析错误:语法错误,C:\wamp 中出现意外的 '[',期待 ']'
    • 你说这个文档包含“key”:“value”
    • @Emmanuel 现在尝试代码,确保 $sites_html 包含您所说的网址:“key”:“value”,如果它们是这样的,您可以使用此代码
    • @hassan 在此先感谢,但它不起作用,如果你可以运行完整的代码并检查它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2013-10-01
    • 2015-09-24
    相关资源
    最近更新 更多