PHP Regular Expressions Extract web hyperlink URLs and pictures in Web pages

Source: Internet
Author: User
    1. function Match_links ($document) {
    2. Preg_match_all ("' <\s*a\s.*?href\s*=\s* ([\ ' \ '])?" (1) (.*?) \\1| ([^\s\>]+)) [^>]*>? (.*?)' Isx ", $document, $links);
    3. while (list ($key, $val) = each ($links [2])) {
    4. if (!empty ($val))
    5. $match [' link '] [] = $val;
    6. }
    7. while (list ($key, $val) = each ($links [3])) {
    8. if (!empty ($val))
    9. $match [' link '] [] = $val;
    10. }
    11. while (list ($key, $val) = each ($links [4])) {
    12. if (!empty ($val))
    13. $match [' content '] = $val;
    14. }
    15. while (list ($key, $val) = each ($links [0])) {
    16. if (!empty ($val))
    17. $match [' All '] [] = $val;
    18. }
    19. return $match;
    20. }
Copy Code

is mainly a regular problem, the following gives an ASP, the multi-Test regular Fetch page link regular

    1. public string Gethref (string htmlcode)
    2. {
    3. String matchvale = "";
    4. String Reg = @ "(h| H) (r| R) (e| E) (f| F) *= * (' | "")? ((\w|\\|\/|\.|:| -|_) +) (' | ' "| *|>)?";
    5. foreach (Match m in Regex.Matches (Htmlcode, Reg))
    6. {
    7. Matchvale + = (m.value). ToLower (). Replace ("href=", ""). Trim () + "| |";
    8. }
    9. return matchvale;
    10. }
Copy Code

Example 2,php function code for downloading remote pictures in content with regular expressions

Using the PHP regular expression to determine the content of the picture, download and save the picture under the domain name program is actually a "thief program" an important part.

This section of the program is just the section that downloads remote images.

  1. if (Preg_match_all ("/http://[^" ']+[.jpg|. Gif|. Jpeg|. Png]+/ui ", Stripcslashes ($content), $aliurl)) {
  2. $i = 0; Multiple files + +
  3. while (list ($key, $v) = each ($aliurl [0])) {
  4. echo $v. "
    ";
  5. $filetype = PathInfo ($v, pathinfo_extension); Get suffix Name
  6. $FF = @file_get_contents ($v); Get 2 binary file contents
  7. if (!stripos ($v, "jbxue.com")) {//Determine if it is a picture under your own website
  8. if (!empty ($FF)) {//Get to file do the following
  9. $dir = "upload/". Date ("Ymd"). " /";//Specify a new storage path
  10. if (!file_exists ($dir)) {//Determine if the directory exists
  11. @mkdir ($dir, 511,true); Create multi-level catalogs, 511 converted to decimal 777 with executable permissions
  12. }//Bbs.it-home.org
  13. $NFN = $dir. Date ("Ymdhis"). $i. ".". $filetype; Build a new name for the file
  14. $NF = @fopen ($nfn, "w"); Create a file
  15. Fwrite ($NF, $FF); Write file
  16. Fclose ($NF); Close File
  17. $i + +; Multi-file + +
  18. echo "";
  19. $content = Str_replace ($v, $NFN, $content);//Replace parameters in content
  20. }else{//the picture is not available, replace it with the default picture
  21. $content = Str_replace ($v, "/upload/201204/20120417213810742.gif", $content);//Replace parameters in content
  22. }
  23. }
  24. }
  25. }
Copy Code

Example 3,php a regular expression to download a picture locally.

  1. /*

  2. Shortage: If the picture path in the page is not an absolute path, you cannot crawl
  3. */
  4. Set_time_limit (0);//crawl is not limited by time

  5. $URL = ' http://pp.baidu.com/';//any URL

  6. Get_pic ($URL);

  7. function Get_pic ($pic _url) {

  8. Get Picture binary stream
  9. $data =curlget ($pic _url);
  10. /* Get a picture link using regular expressions */
  11. $pattern _src = '/<[img| Img].*?src=[\ ' |\ "] (. *? (?: [\.gif|\.jpg])) [\ ' |\ '].*? [\/]?>/';
  12. $num = Preg_match_all ($pattern _src, $data, $match _src);
  13. Get an array of images $arr _src= $match _src[1];//
  14. Get_name ($arr _src);

  15. echo "
    Finished!!! ";

  16. return 0;
  17. }

  18. /* Get the picture type and save it to the same directory as the file */

  19. function get_name ($pic _arr)
  20. {
  21. Type of picture
  22. $pattern _type = '/(/. ( jpg|bmp|jpeg|gif|png))/';

  23. foreach ($pic _arr as $pic _item) {//loops out the address of each picture

  24. $num = Preg_match_all ($pattern _type, $pic _item, $match _type);
  25. $pic _name = Get_unique (). Name of microsecond timestamp $match _type[1][0];//Change
  26. Save a picture as a stream
  27. $write _fd = @fopen ($pic _name, "WB");
  28. @fwrite ($write _fd, Curlget ($pic _item));
  29. @fclose ($write _fd);
  30. echo "[ok]..!";
  31. }
  32. return 0;
  33. }

  34. Get a unique ID through microsecond time

  35. function Get_unique () {
  36. List ($msec, $sec) = Explode ("", Microtime ());
  37. Return $sec. Intval ($msec *1000000);
  38. }

  39. Crawl Web content

  40. function Curlget ($url) {
  41. $url =str_replace (' & ', ' & ', $url);
  42. $curl = Curl_init ();
  43. curl_setopt ($curl, Curlopt_url, $url);
  44. curl_setopt ($curl, Curlopt_header, false);

  45. curl_setopt ($curl, Curlopt_referer, $url);

  46. curl_setopt ($curl, Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 6.0; seaport/1.2; Windows NT 5.1; SV1; infopath.2) ");
  47. curl_setopt ($curl, Curlopt_cookiejar, ' cookie.txt ');
  48. curl_setopt ($curl, Curlopt_cookiefile, ' cookie.txt ');
  49. curl_setopt ($curl, Curlopt_returntransfer, 1);
  50. curl_setopt ($curl, curlopt_followlocation, 0);
  51. $values = curl_exec ($curl);
  52. Curl_close ($curl);
  53. return $values;
  54. }
  55. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.