Jump to content
xisto Community
jlhaslip

Building An Unordered List Of Anchors From a list of files in a folder

Recommended Posts

Building an Unordered List of Anchors from a Folder of files

 

This script reads files from the named folder and then builds a list of links from the filenames using the contents of the first <h3> tag as the link description and the file-name as the anchor href. The file should be a web readable file such as html or php.

This script may come in handy for creating a list of clickable links for use in sidebars and on 'sitemap' pages.

 

Simply build a series of html files and store them all in a single folder, ensuring that the description inside the first h3 html tag is suitable for using as the display field for the anchor list. The Folder name you use must be altered in this script to match your folder name, of course.

This script could be used to generate a list of links in sidebars or on sitemaps. Be creative.

This script is freely available for use by all who recive it.

 

Sample page using this script

 

(php coding based on a script supplied by Tyssen)(Thank you Tyssen)

 

<?php 	$fileCount = 0;$folder = "folder-name-here/"; // change the folder to your folder nameif ($handle = opendir($folder)) {	while (false !== ($file = readdir($handle))) {	if ($file != "." && $file != "..") {$filename =$_SERVER['DOC_ROOT'].$folder.$file;$fp = fopen($filename, "r");$contents = fread($fp, filesize($filename));$pos1 = strpos($contents,"<h3>");// look for the h3 tag position$pos2 = strpos($contents,"</h3>");// look for the ending h3 position$str_length =  $pos2 - $pos1;// calculate the length between tag positions$title = substr($contents,$pos1+4,$str_length-4);// create a string based on tag position and offset$title_array[] = $title;fclose($fp);$file_array[] = $file;$fileCount++;}}}closedir($handle); if ($fileCount > 1) {print "<ul>\n";// start ul tag$iterator = 0;// this section prints the li tags, use css to define the attributes of the ulwhile($title_array[$iterator]) {$file = substr($file_array[$iterator], 0, (strlen($file_array[$iterator])));  print '<li><a href="'.$folder.$file.'">'.$title_array[$iterator].'</a></li>';  $iterator ++;print "\n\n";}print "</ul>\n";unset($file_array);unset($title_array);}?>

Sample File Contents:

 

<head></head>
<title></title>
<body>
<h3>An article name</h3>
</body>

 

Sample page using this script

 

I hope you find this useful. I may spend some more time on this and make a few modifications. If I do, I will post them here.

Share this post


Link to post
Share on other sites

I think some of the coding could be removed, and instead of a while loop, maybe a foreach statement would have been a better choice.

Share this post


Link to post
Share on other sites

(php coding based on a script supplied by Tyssen)(Thank you Tyssen)

Well, I got the bulk of it from someone else on another forum, so I can't really take credit for it. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.