0

I read every single stackoverflow questions and I try each one but nothing works for me.

I have this HTML in the external webpage:

<a href="/gallery/image1.png" id="viewbt">
<div class="view"></div>
</a>

I want to get this whole code using any kind of PHP codes.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Nadine Ah
  • 47
  • 1
  • 1
  • 7
  • 1
    Congratulations for having read 39,012,055 questions! [file_get_contents()](http://php.net/file_get_contents) can accept an external URL to load a web page. If you want to parse out a link from a chunk of HTML code, you may want to read on [preg_match()](http://php.net/preg_match). – rationalboss Aug 18 '16 at 07:19
  • @rationalboss Regular expressions probably shouldn't be used, see https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/. – jedifans Aug 18 '16 at 07:21
  • haha @rationalboss I will. – Nadine Ah Aug 18 '16 at 07:22
  • Consider using DOMDocument to parse the fetched content. – jedifans Aug 18 '16 at 07:24

1 Answers1

0

PHP is server side, while going to a specific HTML tag is a browser feature. There is still a way. You will need to fetch that page and search for the exact tag with regex or with DOM manipulation.

$str = file_get_contents('http://your_php_subpage.com');
// to find the specific tag ( for example the image link )
if(preg_match('#<a href="(.*?)" id="viewbt">.*?</a>#', $str, $m)){
     var_dump($m);
} else {
    echo 'Regex syntax has to be improved to your search criteria'.PHP_EOL;
}

Extra info for regex: http://php.net/preg_match

I suggest regarding a regex tutorial as well, besides the PHP function. You can do a lot more.

Cobuz Alexandru
  • 322
  • 2
  • 11
  • Regex is not a html parser: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ use DOMDocument. – jedifans Aug 18 '16 at 07:25
  • thanks for reply but all what I get is: Regex syntax has to be improved to your search criteria – Nadine Ah Aug 18 '16 at 07:29
  • This means that you hit the exception of regex, are there any special characters that you want to fetch ? – Cobuz Alexandru Aug 18 '16 at 09:13