Php method for parsing all URL addresses in a string _ PHP Tutorial

Source: Internet
Author: User
Php parses all URL addresses in a string. Php parses all URL addresses in a string as follows: 1234567891011121314151617181920212223242526272829303132333435363738394041424 php parses all URL addresses in a string

The details are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

// $ Html = the html on the page

// $ Current_url = the full url that the html came from

// (Only needed for $ repath)

// $ Repath = converts ../and // urls to full valid urls

Function pageLinks ($ html, $ current_url = "", $ repath = false ){

Preg_match_all ("/\

$ Links = array ();

If (isset ($ matches [2]) {

$ Links = $ matches [2];

}

If ($ repath & count ($ links)> 0 & strlen ($ current_url)> 0 ){

$ Pathi = pathinfo ($ current_url );

$ Dir = $ pathi ["dirname"];

$ Base = parse_url ($ current_url );

$ Split_path = explode ("/", $ dir );

$ Url = "";

Foreach ($ links as $ k => $ link ){

If (preg_match ("/^ \. \./", $ link )){

$ Total = substr_count ($ link ,"../");

For ($ I = 0; $ I <$ total; $ I ++ ){

Array_pop ($ split_path );

}

$ Url = implode ("/", $ split_path). "/". str_replace ("../", "", $ link );

} Elseif (preg_match ("/^ \/", $ link )){

$ Url = $ base ["scheme"]. ":". $ link;

} Elseif (preg_match ("/^ \/| ^. \ //", $ link )){

$ Url = $ base ["scheme"]. ": //". $ base ["host"]. $ link;

} Elseif (preg_match ("/^ [a-zA-Z0-9]/", $ link )){

If (preg_match ("/^ http/", $ link )){

$ Url = $ link;

} Else {

$ Url = $ dir. "/". $ link;

}

}

$ Links [$ k] = $ url;

}

}

Return $ links;

}

Header ("content-type: text/plain ");

$ Url = "http://www.jb51.net ";

$ Html = file_get_contents ($ url );

// Gets links from the page:

Print_r (pageLinks ($ html ));

// Gets links from the page and formats them to a full valid url:

Print_r (pageLinks ($ html, $ url, true ));

The specific region is as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 40 41 42 4...

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.