title: How to extract complete sub links using Simple-HTML-DOM?
body: <p>The following is the basic code I use to extract sublinks from a page:</p>
  
  <pre><code>&lt;?php
      include_once('simple_html_dom.php');
      function extract_links($target_url)
      {   
          $html = new simple_html_dom();
          $html-&gt;load_file($target_url);  
          $i=0;
          $crawl =array();
          foreach($html-&gt;find('a') as $link)
          {
              $crawl[$i] = $link-&gt;href;
              $i++;
          }
          var_dump($crawl);
      }
      extract_links('http://stackoverflow.com');
  ?&gt;
  </code></pre>
  
  <p>The output is as follows:</p>
  
  <pre><code>array
    0 =&gt; string 'http://stackexchange.com' (length=24)
    1 =&gt; string '/users/login' (length=12)
    2 =&gt; string 'http://careers.stackoverflow.com' (length=32)
    3 =&gt; string 'http://chat.stackoverflow.com' (length=29)
    4 =&gt; string 'http://meta.stackoverflow.com' (length=29)
    5 =&gt; string '/about' (length=6)
    6 =&gt; string '/faq' (length=4)
    7 =&gt; string '/' (length=1)
    8 =&gt; string '/questions' (length=10)
    9 =&gt; string '/tags' (length=5)
    10 =&gt; string '/users' (length=6)
    11 =&gt; string '/badges' (length=7)
    12 =&gt; string '/unanswered' (length=11)
    13 =&gt; string '/questions/ask' (length=14)
    14 =&gt; string '?tab=interesting' (length=16)
    15 =&gt; string '?tab=featured' (length=13)
    16 =&gt; string '?tab=hot' (length=8)
    17 =&gt; string '?tab=week' (length=9)
    18 =&gt; string '?tab=month' (length=10)
    19 =&gt; string '/questions/14611052/basic-standalone-jpa-example-with-postgres-using-eclipse' (length=76)
    20 =&gt; string '/questions/tagged/eclipse' (length=25)
    21 =&gt; string '/questions/tagged/postgresql' (length=28)
    22 =&gt; string '/questions/tagged/jpa' (length=21)
    23 =&gt; string '/questions/14611052/basic-standalone-jpa-example-with-postgres-using-eclipse' (length=76)
    24 =&gt; string '/users/865448/tostao' (length=20)
    25 =&gt; string '/questions/14611172/unable-to-fully-print-a-page-containing-iframes-in-chrome' (length=77)
    26 =&gt; string '/questions/tagged/javascript' (length=28)
    27 =&gt; string '/questions/tagged/jquery' (length=24)
    28 =&gt; string '/questions/tagged/html' (length=22)
    29 =&gt; string '/questions/tagged/html5' (length=23)
    30 =&gt; string '/questions/tagged/google-chrome' (length=31)
    31 =&gt; string '/questions/14611172/unable-to-fully-print-a-page-containing-iframes-in-chrome' (length=77)
    32 =&gt; string '/users/962868/tejas' (length=19)
    33 =&gt; string '/questions/14609779/how-can-i-configure-bash-to-handle-crlf-shell-scripts' (length=73)
    34 =&gt; string '/questions/tagged/linux' (length=23)
    35 =&gt; string '/questions/tagged/windows' (length=25)
    36 =&gt; string '/questions/tagged/bash' (length=22)
    37 =&gt; string '/questions/tagged/line-endings' (length=30)
    38 =&gt; string '/questions/14609779/how-can-i-configure-bash-to-handle-crlf-shell-scripts/?lastactivity' (length=87)
    39 =&gt; string '/users/1899640/that-other-guy' (length=29)
    40 =&gt; string '/questions/14611169/using-one-socket-for-peer-to-peer-communication' (length=67)
    41 =&gt; string '/questions/tagged/sockets' (length=25)
    42 =&gt; string '/questions/tagged/p2p' (length=21)
    43 =&gt; string '/questions/14611169/using-one-socket-for-peer-to-peer-communication' (length=67)
    44 =&gt; string '/users/911651/xsnrg' (length=19)
    45 =&gt; string '/questions/14611166/possible-mistake-in-ios-dev-guide' (length=53)
    46 =&gt; string '/questions/tagged/iphone' (length=24)
    47 =&gt; string '/questions/tagged/ios' (length=21)
    48 =&gt; string '/questions/tagged/objective-c' (length=29)
    49 =&gt; string '/questions/14611166/possible-mistake-in-ios-dev-guide' (length=53)
    50 =&gt; string '/users/107715/matt-n' (length=20)
    51 =&gt; string '/questions/14611163/how-to-use-dispatcher-in-wpf-to-make-a-timer' (length=64)
    52 =&gt; string '/questions/tagged/wpf' (length=21)
    53 =&gt; string '/questions/tagged/timer' (length=23)
    54 =&gt; string '/questions/tagged/dispatcher' (length=28)
    55 =&gt; string '/questions/14611163/how-to-use-dispatcher-in-wpf-to-make-a-timer' (length=64)
    56 =&gt; string '/users/1741800/nashat' (length=21)
    57 =&gt; string '/questions/14610879/how-can-i-handle-an-access-violation-in-visual-studio-c' (length=75)
    58 =&gt; string '/questions/tagged/visual-c%2b%2b' (length=32)
    59 =&gt; string '/questions/tagged/exception-handling' (length=36)
    60 =&gt; string '/questions/tagged/access-violation' (length=34)
    61 =&gt; string '/questions/tagged/structured-exception' (length=38)
    62 =&gt; string '/questions/14610879/how-can-i-handle-an-access-violation-in-visual-studio-c/?lastactivity' (length=89)
    63 =&gt; string '/users/901812/big-endian' (length=24)
    64 =&gt; string '/questions/14611162/mvc-condintional-authorization' (length=50)
    65 =&gt; string '/questions/tagged/c%23' (length=22)
    66 =&gt; string '/questions/tagged/asp.net-mvc' (length=29)
    67 =&gt; string '/questions/tagged/asp.net-mvc-4' (length=31)
    68 =&gt; string '/questions/tagged/authorization' (length=31)
    69 =&gt; string '/questions/14611162/mvc-condintional-authorization' (length=50)
    70 =&gt; string '/users/644969/cadrell0' (length=22)
    71 =&gt; string '/questions/14611160/get-customer-role-nopcommerce' (length=49)
    72 =&gt; string '/questions/tagged/c%23' (length=22)
    73 =&gt; string '/questions/tagged/razor' (length=23)
    74 =&gt; string '/questions/tagged/nopcommerce' (length=29)
    75 =&gt; string '/questions/14611160/get-customer-role-nopcommerce' (length=49)
    76 =&gt; string '/users/1378841/mlg74' (length=20)
    77 =&gt; string '/questions/14611158/iframe-resizing-nested-in-gridview' (length=54)
    78 =&gt; string '/questions/tagged/resize' (length=24)
    79 =&gt; string '/questions/14611158/iframe-resizing-nested-in-gridview' (length=54)
    80 =&gt; string '/users/2026451/satish-patil' (length=27)
    81 =&gt; string '/questions/14611157/php-how-to-check-the-value-got-this-word-from-a-var' (length=71)
    82 =&gt; string '/questions/tagged/php' (length=21)
    83 =&gt; string '/questions/tagged/preg-match' (length=28)
    84 =&gt; string '/questions/tagged/strpos' (length=24)
    85 =&gt; string '/questions/14611157/php-how-to-check-the-value-got-this-word-from-a-var' (length=71)
    86 =&gt; string '/users/963414/samual99' (length=22)
    87 =&gt; string '/questions/14611155/how-to-get-the-coordinates-of-boundries-of-drawable-on-the-mapview' (length=86)
    88 =&gt; string '/questions/tagged/android' (length=25)
    89 =&gt; string '/questions/tagged/google-maps' (length=29)
    90 =&gt; string '/questions/14611155/how-to-get-the-coordinates-of-boundries-of-drawable-on-the-mapview' (length=86)
    91 =&gt; string '/users/1520564/blubar' (length=21)
    92 =&gt; string '/questions/14611153/why-css-is-empty-when-ssl-is-on-and-appcache-is-enabled-ipad-safari' (length=87)
    93 =&gt; string '/questions/tagged/css' (length=21)
    94 =&gt; string '/questions/tagged/ipad' (length=22)
    95 =&gt; string '/questions/tagged/ssl' (length=21)
    96 =&gt; string '/questions/tagged/mobile-safari' (length=31)
    97 =&gt; string '/questions/tagged/html5-appcache' (length=32)
    98 =&gt; string '/questions/14611153/why-css-is-empty-when-ssl-is-on-and-appcache-is-enabled-ipad-safari' (length=87)
    99 =&gt; string '/users/2026375/twoface' (length=22)
    100 =&gt; string '/questions/14611149/laravel-how-to-temporarily-store-eloquent-models-in-db-without-a-proper-schem' (length=97)
    101 =&gt; string '/questions/tagged/php' (length=21)
    102 =&gt; string '/questions/tagged/laravel' (length=25)
    103 =&gt; string '/questions/14611149/laravel-how-to-temporarily-store-eloquent-models-in-db-without-a-proper-schem' (length=97)
    104 =&gt; string '/users/291557/duality' (length=21)
    105 =&gt; string '/questions/13928812/xmlserializer-generateserializer-and-collections' (length=68)
    106 =&gt; string '/questions/tagged/c%23' (length=22)
    107 =&gt; string '/questions/tagged/xml-serialization' (length=35)
    108 =&gt; string '/questions/13928812/xmlserializer-generateserializer-and-collections/?lastactivity' (length=82)
    109 =&gt; string '/users/1200614/phil' (length=19)
    110 =&gt; string '/questions/14611145/keep-buttons-in-view-when-keyboard-opens-android' (length=68)
    111 =&gt; string '/questions/tagged/android' (length=25)
    112 =&gt; string '/questions/tagged/keyboard' (length=26)
    113 =&gt; string '/questions/tagged/resize' (length=24)
    114 =&gt; string '/questions/tagged/window' (length=24)
    115 =&gt; string '/questions/tagged/views' (length=23)
    116 =&gt; string '/questions/14611145/keep-buttons-in-view-when-keyboard-opens-android' (length=68)
    117 =&gt; string '/users/1137413/725623452362' (length=27)
    118 =&gt; string '/questions/14611144/ssdp-discovery-from-a-browser' (length=49)
    119 =&gt; string '/questions/tagged/silverlight' (length=29)
    120 =&gt; string '/questions/tagged/flash' (length=23)
    121 =&gt; string '/questions/14611144/ssdp-discovery-from-a-browser' (length=49)
    122 =&gt; string '/users/191882/legege' (length=20)
    123 =&gt; string '/questions/14611143/how-to-syncrhonize-on-site-in-memory-no-sql-datasources-with-central-database-in' (length=100)
    124 =&gt; string '/questions/tagged/architecture' (length=30)
    125 =&gt; string '/questions/tagged/nosql' (length=23)
    126 =&gt; string '/questions/tagged/java-ee-6' (length=27)
    127 =&gt; string '/questions/tagged/in-memory-database' (length=36)
    more elements...
  </code></pre>
  
  <p>Now consider '<strong>/about</strong>' sublink in the array. I want it to be displayed as '<strong><a href="https://stackoverflow.com/about">https://stackoverflow.com/about</a></strong>'. Why only subpart of sublink is returned while in some cases complete sublink is returned ?
  Also some links are starting with '<strong>?</strong>' sign. How to sanitize these links ?</p>
  
  <p><strong>EDIT:</strong>
  Consider "<strong><code>http://en.wikipedia.org/wiki/Web_crawler</code></strong>". Now if I perform extract_links on it, I get a sublink like this "<strong><code>http://en.wikipedia.org/wiki/Web_crawler/wiki/Web_search_engine</code></strong>" which is invalid and most of the links are of this format. The correct link is "<strong><code>http://en.wikipedia.org/wiki/Web_search_engine</code></strong>". <strong>And I am using this function in another program which will pass an array of links so I cannot keep the if conditions <em>static</em></strong>. The following is the code fragment I am using now:</p>
  
  <pre><code>foreach($html-&gt;find('a') as $link)
  {   
      $href = $link-&gt;href;
      $fchr = substr($href, 0, 1);
      if ($fchr === '/')
      {
          $href = $target_url.$href;
      }
      else if ($fchr === '?')
      {
          $href = $target_url.'/'. $href;
      }
  }
  </code></pre>