abhi_madhani Posted May 26, 2012 Share Posted May 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/263177-recursive-search-in-html-page/ Share on other sites More sharing options...
abhi_madhani Posted May 27, 2012 Author Share Posted May 27, 2012 Hi, Any help would be appreciated in making Recursive function for this code. Regards, Abhishek Quote Link to comment https://forums.phpfreaks.com/topic/263177-recursive-search-in-html-page/#findComment-1349011 Share on other sites More sharing options...
abhi_madhani Posted May 28, 2012 Author Share Posted May 28, 2012 Hi People, Please let me know is my introduction of problem not well presented or am I trying to do something that is not possible. Regards, Abhishek Quote Link to comment https://forums.phpfreaks.com/topic/263177-recursive-search-in-html-page/#findComment-1349214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.