Search code examples
phpurlcurlstrpos

php strpos multiple, string inside html tag


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'
              }

Solution

  • 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("\n",$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 />";
    }