Home  >  Article  >  Backend Development  >  Find PHP code for all links in a page

Find PHP code for all links in a page

WBOY
WBOYOriginal
2016-07-25 08:46:01941browse

  1. function get_links($link) {
  2. $html = file_get_contents($link);
  3. $html = str_replace("n", "", $html);
  4. $html = preg_replace('/ $html = preg_replace('//', "n", $html);
  5. preg_match_all('/< as*.*>.*?/', $html, $matches);
  6. return($matches);
  7. }
Copy code

In this example, we want to use file_get_contents to get the content of a web page. Then use str_replace("n", "", $html) to remove all line breaks. Then use preg_replace('//', "n", $html) to put all .... patterns on a new line. Finally, use preg_match_all('/.*?/', $html, $matches) to match the link pattern. /.*?/ is a regular expression that matches this pattern. So why do we put the ..... link on a new line? ? Because in the /.*?/ pattern, .* cannot match newlines, so if and are not on the same line, they cannot match! ! So we're going to do this!
PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php display errorNext article:php display error