2

I am using PHP Simple HTML DOM Parser to fetch urls, but i got an error while fetching links. Have a look at this script:

$result = str_get_html($result);
foreach($result->find('a') as $element)
$result = str_get_html($result);
$result = str_replace('http://', '', $result);
foreach($result->find('a') as $elementa)
echo $element->href;
echo $elementa->href;

Here I want to fetch all links for twice, first time urls in $element->href will fetch links starting with http:// and in $elementa->href will fetch links without http://.

But this shows only a blank page. Any idea?

3 Answers 3

1
$result = str_get_html($result);
$arrWithPrefix = array();
$arrWithoutPrefix = array();
foreach ($result->find('a') as $link) {
    $arrWithPrefix[] = $link->href;
    $arrWithoutPrefix[] = str_replace('http://', '', $link->href);
}
var_dump($arrWithPrefix);
var_dump($arrWithoutPrefix);

Not tested, see if it is any good :)

Sign up to request clarification or add additional context in comments.

6 Comments

Do you want the entire <A> tag for each element ?
Could you please edit your question with a sample input HTML and the unexpected output from the above script ?
Thanks for your reply.. please take some time visiting here codepad.org/TWIYdkX3 and look what to do to do the exact i want to do.. Thanks again
Skimming, I saw that you're pasting the component source code in your script (I presume for running the app on codepad). Try this: in the extracted folder for the zip file you downloaded from their website, copy and paste this to a test.php file inside the folder app. Notice that it refers to the php file in the folder above it, so be certain that it is there. Try this and then tell us what happens. **Edit: fixed source.
thanks, but i ama php learner so cant understand why it showing lots of texts like } array(16) { [0]=> string(161) " .... how to make it clean .Thanks
|
1

you can also use this code it will set the http:// sitename to the link and will return all links with one link

foreach ($html->find('a') as $e) {
   $cssHrefs = $e -> href;
   preg_match_all('~' . SITE_NAME . '~is', $cssHrefs, $match);
   if (count($match[0]) == 0) {
        $loadedHrefs[] = SITE_NAME . $cssHrefs;
   } else {
        $loadedHrefs[] = $cssHrefs;
}
var_dump($loadedHrefs);

Comments

0

u may try this as well

$result = str_get_html($result);
foreach($result->find('a') as $element){
$result = str_get_html($result);
$result = str_replace('http://', '', $result);
}
foreach($result->find('a') as $elementa){
echo $element->href;
echo $elementa->href;
}

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.