-3

Possible Duplicate:
How to parse and process HTML with PHP?

i get page with file_get_content and i want extract all link in page any way to do this? or can i use str with start an end phares to get target string like this :

$str=fdgdfbfbmnlmnjkl njnkhvnbn j<a href="http://www.google.com">google</a>
$link=str($str,"start","END")??????????
EX : $link=str($str,"http://www","com")=Res=>http://www.google.com or google?

or

$str=file_get_content("http://www.google.com");
    $link=str($str,"start","END")??????????
    EX : $link=str($str,"http://www","com")=Res=>http://www.google.com or google?
Community
  • 1
  • 1
behzad n
  • 239
  • 3
  • 5
  • 15

2 Answers2

1

I had the same problem some time ago. This solution worked very well for me.

 $string = "Hello World, <a href='http://www.google.com'>Google</a> ! Search also on <a href='http://www.bing.com'>Bing</a>";

 preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);

 $matches = $match[0];

 foreach($matches as $var)
 {    
     print($var."<br>"); 
 }
nullpointr
  • 524
  • 4
  • 18
0

You should use DOM methods to extract content from HTML - using regular expressions will result in madness:

<?php
    $dom = new DOMDocument;
    $dom->loadHTMLFile('http://www.google.com/');

    $a = $dom->getElementsByTagName('a');
    foreach ($a as $e) {
        echo $e->getAttribute("href") . "\n";
    }
?>
Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495