[PHP] preg_match_all: links fetchen

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • TangLeFuzZ
  • Registratie: Juni 2001
  • Laatst online: 28-05-2024
Hey,

ik kwam hier op de tweede pagina een leuk interessante code tegen, die links uit een bepaalde pagina haalt en weergeeft.

PHP:
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
function print_links ($url) 
{ 
$fp = fopen($url, "r") 
or die("Could not contact $url"); 
$page_contents = ""; 
while ($new_text = fread($fp, 100)) { 
$page_contents .= $new_text; 
} 
$match_result = 
preg_match_all('/<\s*A\s*HREF="([^\"]+)"\s*>([^>]*)<\/A>/i',$page_contents,$match_array,PREG_SET_ORDER); 

foreach ($match_array as $entry) { 
$href = $entry[1]; 
$anchortext = $entry[2]; 
$lcheck = substr($href, 0, 1); 
if($lcheck == "h"){ 
print("<a href=\"$href\">$anchortext</a><br>"); 
}elseif($lcheck == "/"){ 
$hreffix = substr($href, 1, 250); 
print("<a href=\"$url/$hreffix\">$anchortext</a><br>\n"); 
}else{ 
print("<a href=\"$url/$href\">$anchortext</a><br>"); 
} 
} 
}


Nu ben ik hier wat mee aan het kloten, het werkt mooi, maar niet wanneer links nog een extra attribuut behalve href hebben (bijvoorbeeld <a href="blaat.php" class=test>

Het gaat dus hier om:

PHP:
1
preg_match_all('/<\s*A\s*HREF="([^\"]+)"\s*>([^>]*)<\/A>/i',$page_contents,$match_array,PREG_SET_ORDER);


Weet iemand hoe dit op te lossen is?

Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

PHP:
1
preg_match_all('/<\s*A\s*[^>]+?HREF="([^\"]+)"\s*[^>+]?>(.*)<\/A>/i',$page_contents,$match_array,PREG_SET_ORDER);

Je kan natuurlijk ook in de FAQ kijken, staat een leuke regular expression tutorial inclusief links.

[ Voor 15% gewijzigd door NMe op 03-11-2004 20:42 ]

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.