Help with Counting Links on a web page.

Hello everyone, I’m here with a question… I’ve searched high and low for a way to do this and oddly enough, there have been no results in finding any examples of the script I’m trying to accomplish.

Let me set this up for you all first… I have a web site that uses a template… The pages are php… And for example on my navigation bar I have links to other pages…


Now what I am attempting to accomplish is to have it show how many links are on each of those pages next to the link in the template… example…

Dogs (5)
Cats (3)
Birds (2)
Snakes (3)

Now I’ve came up with this…

function countLinks($myfile)
	$fh = fopen($myfile,'r');
	$theData = fgets($fh);
	$matches = array();
	if( preg_match_all("/href/i", $theData, $matches))
		echo "(" . $matches[0] . ")";
		echo "(0)";

Dogs<?php countLinks("dogs.php") ?>
Cats<?php countLinks("cats.php") ?>
Birds<?php countLinks("birds.php") ?>
Snakes<?php countLinks("snakes.php") ?>

I just keep getting (0) as the outcome… Any help with this would greatly be appreciated.

Thank you.

after calling preg_match_all $maches[0] holds an array of the first match.
so u would have to use count($maches) instead of $maches[0].
but i would use
[php]$matches = array();
echo “(” . intval(preg_match_all("/href/i", $theData, $matches)) . “)”;[/php]

fgets() only reads one line. use fread() instead.

if it’s still not working try to debug.
echo($theData) and print_r($matches)

and feel free to ask again

Sponsor our Newsletter | Privacy Policy | Terms of Service