Hi! I want to writa a script saving links from one site. There are over 30 link in one page, the have got the same divs. What php function should I use to get the links ? Thanks.
Hi, May be you have to use preg_match but if you place here input string and what output string have to be will be much better. ps: This thread is similar. Regards
Hi, if you're unfamiliar with regular expressions, you could do it another way (a bit cranky): use strip_tags function to get page code without tag text, then use explode function with blank space argument to break page code into separate words and then strpos to determine which of words have "http://" prefix. Regards
// I use curl but file_get_contents will work for most domains $html = file_get_contents('http://news.google.com/'); $dom = new DOMDocument(); @$dom->loadHTML($html); $links = $dom -> getElementsByTagName('a'); foreach ($links as $link) { $anchor_text = $link -> firstChild -> data; $href = $link ->getAttribute('href'); echo '<a href="' . $href . '">' . $anchor_text . '</a><br />'; } PHP:
If I get this right, you could make some changes to JDevereux excellent code: $specified_ending = ".htm"; // this is the specified ending you mentioned, change it at will $dom = new DOMDocument(); @$dom->loadHTML($html); // $html is your input HTML code $links = $dom->getElementsByTagName('a'); $wanted_links = array(); foreach ($links as $link) { $href = $link->getAttribute('href'); if (strpos($href, $specified_ending) == strlen($href) - strlen($specified_ending)) { $wanted_links[] = $href; } } PHP: This way you can specify the desired ending of links and all appropriate links are stored in an array $wanted_links.