Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 892617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:05:10+00:00 2026-05-15T14:05:10+00:00

I have the code to extract the links, but I need the link label

  • 0

I have the code to extract the links, but I need the link label also. I need links to be stored in an array and link label to be stored in another array.

for example if the site bbc.com has the code <a href="bbc.com/sports.html>sports</a>, I need $linklabel[0]=sports and $link[0]=bbc.com/sports.html.

The code is below, but error occurs as Fatal error: Call to undefined method DOMXPath::find() in C:\wamp\www\test\d.php on line 14

<?php
    $url='http://edition.cnn.com/?fbid=4OofUbASN5k';

    $var = fread_url($url);// function calling to get the page from curl
    $search = array('@<script[^>]*?>.*?</script>@si');  // Strip out javascript
    $var = preg_replace($search, "\n", html_entity_decode($var)); // Strip out javascript

    $linklabel = array();
    $link = array();
    $dom = new DOMDocument($var);
    @$dom->loadHTML($var);
    $xpath = new DOMXPath($dom);// Grab the DOM nodes 

foreach($xpath->find('a') as $element)
   {
     array_push($linklabel, $element->innerText);
     print $linklabel;
     array_push($link, $element->href);
     print $link.'<br>';
    }


    function fread_url($url)
    {
        if(function_exists("curl_init")){
            $ch = curl_init();
            $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
                          "Windows NT 5.0)";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
            curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
            curl_setopt( $ch, CURLOPT_URL, $url );

            curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
            $html = curl_exec($ch);
            //print $html;//printing the web page.
            curl_close($ch);
        }
        else{
            $hfile = fopen($url,"r");
            if($hfile){
                while(!feof($hfile)){
                    $html.=fgets($hfile,1024);
                }
            }
        }
        return $html;
    }

?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:05:10+00:00Added an answer on May 15, 2026 at 2:05 pm

    This is easy using the Simple HTML DOM.

    $html = file_get_html('http://www.google.com/');
    
    $linklabel = array();
    $link = array();
    
    foreach($html->find('a') as $element)
       {
         array_push($linklabel, $element->innerText);
         array_push($link, $element->href);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html code stored in a string. Now I want to extract
So for example, I have a list and I need to extract the hrefs
I have the following code that I am trying to extract the systems proxy
I have to extract defined variable and function names from a js code passed
I have been trying to extract e-mail addresses from the following code for quite
I have code that generates a List<string[]> variable but can't quite figure out how
I have code that looks more or less like the code below but it
I have written some C++ code that generates a std::vector. I also have a
I need to have the white blocks respond to click and hover events but
This is python code for capturing streaming video from server. but I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.