HTML
|
|
|
|
<?php
// before we start, put your HTML code (as a string) into $data.
$data = "This is my <A HREF="http://thechain.com/">HTML</A> code.";
unset($location);
$links = array(); // the contents of the anchors (highlighted text)
$hrefs = array(); // the URLs corresponding to each element of $links
$pos = 0;
while (!(($pos = strpos($data,"<",$pos)) === false)) {
$pos++;
$endpos = strpos($data,">",$pos);
$tag = substr($data,$pos,$endpos-$pos);
$tag = trim($tag);
if (isset($location)) { // look for a </A>
if (!strcasecmp(strtok($tag," "),"/A")) {
$link = substr($data,$linkpos,$pos-1-$linkpos);
$links[] = $link;
$hrefs[] = $location;
unset($location);
}
$pos = $endpos+1;
} else { // look for a <A ...>
if (!strcasecmp(strtok($tag," "),"A")) {
if (eregi("HREF[ tnrv]*=[ tnrv]*"([^"]*)"",$tag,$regs));
else if (eregi("HREF[ tnrv]*=[ tnrv]*([^ tnrv]*)",$tag,$regs));
else $regs[1] = "";
if ($regs[1]) { // Only use it if it seems to be reasonable
$location = $regs[1];
}
$pos = $endpos+1;
$linkpos = $pos;
} else {
$pos = $endpos+1;
}
}
}
?>
|
|
|
Usage Example
|
/* stick this after the above code and you'll
get a list of all the links. */
for ($i=0; $i<sizeof($links); $i++) {
echo "<A HREF="".$hrefs[$i]."">".$links[$i]."</A><BR>n";
}
|
|
|
Rate This Script
|
|
|
|