Jump to content

Recursive search in HTML page


abhi_madhani

Recommended Posts

Hi Phpfreaks,

 

I have written this following code, it take any given URL of, and searches and list all 'ABC' links found in that given page. Then it goes through each 'ABC' link or page, searches and list all 'XYZ' links. The code works perfectly till here.

 

Now I am planning to recursive search, where if more 'ABC' links are present inside each 'ABC' page, it should search and list them and 'XYZ' links, and this could go to many levels deep. It is more or less similar to 'Recursive Directory Listing' which lists the directory structure of folder and files, but in HTML page.

 

I am still learning PHP Basic's, so any help even in simplifying my method of code would be grateful.

I have attached the dummy file that I have been working on, kindly find it attached.

 

Example ::

 

ABC page

ABC Link 1
	XYZ Link 1.1
	XYZ Link 1.2
	XYZ Link 1.3

ABC Link 2
	XYZ Link 2.1
	XYZ Link 2.2

ABC Link 3
	ABC Link 3-1
		XYZ Link 3-1.1
		XYZ Link 3-1.2
		XYZ Link 3-1.3

	ABC Link 3-2
		XYZ Link 3-2.1
		XYZ Link 3-2.1
		XYZ Link 3-2.1

	XYZ Link 3.1
	XYZ Link 3.2

ABC Link 4

 

 

My code is as follows ......

 

<?php

$url = "abclistA.html";
$needle = "abc"; $needle1 = "xyz";
echo '<table border="1">';

ABCfound:
$acontents = file_get_contents($url);

if (strpos($acontents, $needle)==!false) {
	preg_match_all('/id\s*=\s*\"abcs\".*?<\/table>a/isU',$acontents,$amatches);
	preg_match_all('/<a\s*href\s*=\s*\"(.*)\"\s*>\s*(.*)<\/a>/iSU',$amatches[0][0],$abc);

	foreach ($abc[0] as $akey => $abcrecord) {
		echo '<tr><td>'.$abc[2][$akey].'</td></tr>';

		$xcontents = file_get_contents($abc[1][$akey]);
		if(strpos($xcontents, $needle1)!==false) {

			preg_match_all('/id\s*=\s*\"xyz\".*?<\/table>x/isU',$xcontents,$xmatches);
			preg_match_all('/<a\s*href\s*=\s*\"(.*)\"\s*>\s*(.*)<\/a>/iSU',$xmatches[0][0],$xyz);

			foreach ($xyz[0] as $xkey => $xyzrecord) {
				echo '<tr><td>'.$xyz[2][$xkey].'</td></tr>';
			}

			// Planning to use this 'goto' shortcut, that when it again find ABC link inside ABC page, 
			// 	it should go to begining of this code and rerun it.
			// However this time $url will be changed with suburl that needs to scanned.
			// This doesn't seem to work.
			/*if (strpos($xcontents, $needle)==!false) {
				$url = $abc[1][$akey];
				echo " S-MF ";
				goto ABCfound;
			} else { echo " S-MNF "; } */

		} else { echo " SNF ";	}

	}
} else { echo " MNF "; }

echo '</table>';

?>

18464_.zip

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.