Menu
Hey, guys, I’ve got yet another question. I was thinking today, and wondering… How would I use PHP to go to a page and follow all of the links on that page and print their URIs? Any ideas? I haven’t come up with anything, but I am definitely ready and willing to learn this new field.
Thanks, ?
Jona
[code=php]$look=ereg(<a [a-zA-Z0-9]* href="[a-zA-Z0-9]*" [a-zA-Z0-9]*>[a-zA-Z0-9]*</a>);[/code]
[code=php]<?php
$code = file ('http://www.yahoo.com/'); // file to open
foreach ($code as $line_num => $line) { // loop through lines
echo "<span style="font-weight:bold;">Line #$line_num :</span> " . htmlspecialchars($line) . "<br>n"; // echo lines to screen. Note htmlspecialchars() convers special characters to their HTML entities
}
?>[/code]
HTML is easy to learn[/QUOTE]I suppose it depends on how you code - I found programming fairly easy to pick up, but learning to hard-code HTML took longer. HTML does have editors though, which makes it a lot easier.
[code=php]<html>
<head>
<title>Link Validator</title>
<style type="text/css">
a {
color:darkblue;
}
</style>
</head>
<body>
Input a full url (ie. http://www.infinitypages.com/index.php).
<form action="checklinks.php" method="post">
<input type="text" name="url" size="50">
<input type="submit" name="submit" value="Check links">
</form>
<?php
#######################################################
# This script is Copyright 2003, Infinity Web Design #
# Written by Ryan Brill - [email protected] #
# All Rights Reserved - Do not remove this notice #
#######################################################
if ($_POST["url"]) {
$file = $_POST["url"];
echo "Links in file <a href="$file">$file</a>:<br/><br/>n";
$x = 1;
$valid = 0;
$invalid = 0;
$filename = split("/",$file);
$filename = $filename[count($filename)-1];
$path = split($filename, $file);
$path = $path[0];
$contents = @file($file) or die ("Failed to open <a href="$file">$file</a> to check links. Please be sure it is an absolute URL.");
foreach ($contents as $line_num => $line) {
if (preg_match('/<a href=.*?>/', $line, $a)) {
for ($i = 0; $i < count($a); $i++) {
$url = preg_split("/href=['"]/", $a[$i]);
$url2 = preg_split("/['"]/", $url[1]);
$spliturl = parse_url($url2[0]);
if ($spliturl[scheme] == "") {
$finalurl = $path.$url2[0];
}
else {
$finalurl = $url2[0];
}
if (strtolower($spliturl[scheme]) != "mailto") {
$code = @file ($finalurl);// file to open
if (!$code) {
echo "<span style="color:darkred;">Invalid:</span> <a href="$finalurl">$finalurl</a><br/>n";
$invalid++;
}
else {
echo "<span style="color:green;">Valid:</span> <a href="$finalurl">$finalurl</a><br/>n";
$valid++;
}
}
}
}
}
}
if ($x == 1) {
echo "<br/>n";
echo $valid+$invalid." links checked.<br/>n";
if ($valid > 0) {
echo "<span style="color:green;">$valid valid links found.</span><br/>n"; }
if ($invalid > 0) {
echo "<span style="color:darkred;">$invalid invalid links found.</span><br/>n";
}
}
?>
</body>
</html>[/code]
0.1.9 — BETA 6.18