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

Find PHP code for all links in a page

WBOY
Release: 2016-07-25 08:46:01
Original
972 people have browsed it

  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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template