DJ Mike's Tutorials: PHP


< ^ >

Working With Files

Link Extracter

The Javascript stripper used preg_replace() to find and replace Javascript. To turn it into a link extracter you use preg_match_all() find all the links and store them in an array:

preg_match_all( "@<a[^>]*>[^>]*(/a>)@i", $file, $links_list);



Analysis
@ The character I chose for a delimiter
<a Pattern starts with "<a"
[^>]* Followed by any length string of characters not a ">"
> Followed by a ">"
[^>]* Followed by any length string of characters not a ">"
(/a>) Ending in "/a>"
i Case insensitive
$file The string being tested
$links_list The array that holds the results


Now that we have all of the links in an array, we can use foreach() to loop through the array and output the links:

foreach( $links_list[0] as =>)
{ echo "$var<br />\n"; } 

Note that like the Javascript stripper, it adds a base href to the results to fix broken relative links so you need to use an absolute URL for the form action.



Show Example | Show Code | Hide

Show Example | Show Code | Hide

< ^ >



Created by DJ Mike from Santa Barbara

DJ Mike


Dance Away Santa Barbara's Home Page
<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>