Skip to Main Content
PHP 5 Kochbuch, Third Edition
book

PHP 5 Kochbuch, Third Edition

by David Sklar, Adam Trachtenberg, Carsten Lucke, Matthias Brusdeylins, Ulrich Speidel, Stephan Schmidt
September 2009
Intermediate to advanced content levelIntermediate to advanced
912 pages
48h 11m
German
O'Reilly Verlag
Content preview from PHP 5 Kochbuch, Third Edition

13.10 Links aus einer HTML-Datei extrahieren

Problem

Sie möchten die innerhalb eines HTML-Dokuments enthaltenen URLs herausziehen.

Lösung

Verwenden Sie die in Listing 13.10 dargestellte Funktion pc_link_extractor( ).

function pc_link_extractor($s) {
  $a = array();
  if (preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i',
                     $s,$matches,PREG_SET_ORDER)) {
    foreach($matches as $match) {
      array_push($a,array($match[1],$match[2]));
    }
  }
  return $a;
}

Listing 13.10 pc_link_extractor( )

Zum Beispiel:

$links = pc_link_extractor($page);

Diskussion

Die Funktion pc_link_extractor( ) gibt ein Array zurück. Jedes Element dieses Arrays ist selbst wiederum ein Array, dessen erstes Element das Ziel des Links und dessen zweites Element den verlinkten Text ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP programmieren unter Windows

PHP programmieren unter Windows

Arno Hollosi
CSS Kochbuch, 3rd Edition

CSS Kochbuch, 3rd Edition

Joergen Lang, Christopher Schmitt

Publisher Resources

ISBN: 9783868993271Purchase book