0

How to get the contents of an href attribute on an HTML page, based on simple HTML DOM. Here is an example of my script:

<?php
/* update your path accordingly */
include_once 'simple_html_dom.php';

$url = "http://www.codesphp.com/";
$html = file_get_html($url);
$ret =  $html->find('span.titleSnnipets');



foreach($ret as $story){
    echo $story->find('a',0).'<br/>';
}
?>

this script retrieve all tags on the page,i try to retrieve the contents of the attribute of all links.

2
  • What is this, I don't even... Commented Mar 21, 2012 at 21:16
  • Since we can't access your localhost, we can't see the content. This is difficult to help you with. Commented Mar 21, 2012 at 21:19

2 Answers 2

3

Since we can't see the content of your localhost, it is difficult to help you with. I only saw one small thing that I know could be optimized, and that is the first find() call.

<?php
include_once 'simple_html_dom.php';

$url = "http://localhost/website/";
$html = file_get_html($url);
$ret =  $html->find('span.title');

foreach($ret as $story)
{
    echo $story->find('a',0).'<br/>';
}
?>

Update: I already said, since I can't see the content you are using, it is difficult to help you. Your response is to just ask for help again, but make no effort to provide the content? Since you refuse to make your content accessible, I have rewritten your code to use google instead. This example works as expected. Take it and modify it as you need. Until you provide your content, I cannot help you any further.

<?php
include_once 'simple_html_dom.php';
$html = file_get_html('http://www.google.com/');
$divs = $html->find('div.gbm');
foreach($divs as $element)
{
    echo $element->find('a', 0).'<br>';
}
?>

Update #2: This will pull all of the links on the page and display them.

<?php
include_once 'simple_html_dom.php';
$html = file_get_html('http://www.codesphp.com/');
$links = $html->find('a');
foreach($links as $link)
{
    echo $link->href.'<br>';
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

it's good I corrected the code, so what is the solution to my problem
I have already corrected the first post,i put the link to my website codesphp.com, but I want to retrieve the contents of the href attribute for all links.
You are welcome. If my answers and comments helped you, please consider upvoting them.
2

Form what i understand you want to get the 'href' of every link contained in a 'span' with class 'titleSnnipets', if so you can do it like this

<?php
/* update your path accordingly */
include_once 'simple_html_dom.php';

$url = "http://www.codesphp.com/";
$html = file_get_html($url);
$ret =  $html->find('span.titleSnnipets');

foreach($ret as $elements) {
    foreach($elements->find('a') as $link) {
        echo $link->href . '<br>';
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.