doumeng06063991 2014-05-18 17:12
浏览 48
已采纳

php strpos multiple,html标签内的字符串

my Domainname from input is:

example.com

curl $data gives me this, always different:

between <span></span> is the domain name.

1. <span>example.com</span> 2. <span>www.example.com</span> 3. <span>http://example.com</span> 4. <span>https://example.com</span> 5. <span>https://example.com/</span> 6. <span>https://example.com/categories</span> 7. <span><b>www</b>.<b>example</b>.<b>com</b></span> 8. <span><b>www</b>.<b>example.com</b></span> 9. <span>Not included</span>

the professionals have a good solution for me? fals one is found should be stopped.

My current code fails unfortunately. :-(

              $dname = array($d1, $d2, $d3, $d4, $d5, $d6, $d7, $d8, $d9);

              $pos = strpos($data, $dname);

              if ($pos !== false) {
                 echo 'YES';
              } else { 
                 echo 'NO'
              }
  • 写回答

2条回答 默认 最新

  • donglieshe4692 2014-05-18 18:09
    关注

    I personally would use regex:

    preg_match_all('~<span>(.*)</span>~Ui',$data,$domains);
    print_r($domains[1]);
    

    That would give you list of domain names.

    Or in your case, you could use function called strip_tags(), this one would simply remove span tags, and leave what was inside. Then you could simply explode(" ",$stripped_string);

    Aafter that, you can use function in_array() that would check if given domain is in array or not, or... array_diff(), that would compare array with given and return elements that are not in there.

    For example:

    $data = <curl_result>;
    preg_match_all('~<span>(.*)</span>~Ui',$data,$domains); // this finds all domains in span tags
    $domains = array_map('trim',$domains[1]); // this rewrites results and removes blank spaces
    $seeking = array('domain1.com','domain2.com');
    foreach($seeking as $needle){
       if(in_array($needle,$domains) !== false)
          echo "Domain $needle was found!<br />";
       else
          echo "Domain $needle was not found!<br />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败