kektex Posted October 6, 2007 Share Posted October 6, 2007 Hello, I have been searching all afternoon for an answer to this and haven´t found a solution so my last resort is asking here I am collecting links from a few documents on our site using this: <?php $data = file_get_contents('http://www.example.com/'); preg_match_all("/out\.php\?url\=(.+?)\" target/",$data,$match); preg_match_all("/class\=\"id\" title\=\"(.+?)\"/",$data,$desc); foreach($match[1] as $url ) && foreach($desc[1] as $fulldesc){ echo urldecode($url); echo "\| $fulldesc<br>"; } ?> Basically , I want to output all the links and their respective descriptions separated by a pipe character. But reading from the two arrays is giving me a bit of a hard time.I now know I cannot use the && operator, I just left it in so you guys could see what I´m trying to do. How can I do this? I´m not much of a programmer but from what I have read the other option I have is using multi-dimensional arrays? I might be extracting the URL, the description and then some more data later on so this might be a better approach. Thanks for any help or advice. Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/ Share on other sites More sharing options...
kektex Posted October 6, 2007 Author Share Posted October 6, 2007 Bump for me Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363375 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 do you have an example of what you want to pull out i assume your looking for a url and pulling out the title, maybe searching for href would be better! Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363381 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Share Posted October 6, 2007 your having the same problem as me :-\ http://www.phpfreaks.com/forums/index.php/topic,161819.30.html the only thing is, I am already using multi-dimensional arrays. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363383 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 quick example, maybe usless (untested) <?php $data = file_get_contents('http://www.example.com/'); //Find <a> tags preg_match_all('%<a.*</a>%i', $data, $result, PREG_PATTERN_ORDER); $aTags = $result[0]; //Loop through tags foreach($aTags as $aTag) { //find titles if (preg_match('/title=([\s"\'])*(.*?)\1/i', $aTag, $regs)) { $title = $regs[2]; } else { $title = ""; } //find href if (preg_match('/href=([\s"\'])*(.*?)\1/i', $aTag, $regs)) { $href = $regs[2]; } else { $href = ""; } //display results echo "found: $href with the title: $title"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363400 Share on other sites More sharing options...
kektex Posted October 6, 2007 Author Share Posted October 6, 2007 Actually my problem is not the regex or finding the URLs and descriptions, my code already does that. The problem I´m having is outputting the URL and it´s respective anchor text to a text file. An example link: <a href="out.php?url=http://www.example.com/exampledocument.html" target="_blank" class="id" title="LINK DESCRIPTION">Link Description</a> I managed to pull the link from the a href without any problems as well as the LINK DESCRIPTION ( I could have also taken it from the anchor text, they are the same.) What I want to do is output both things to a text file like this: http://www.example.com/exampledocument.html | LINK DESCRIPTION Is there any way to use foreach to do this or should I try to use a multi-dimensional array (not that I know how to do that anyway but I'll just read up on it). Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363439 Share on other sites More sharing options...
BlueSkyIS Posted October 6, 2007 Share Posted October 6, 2007 as you're finding links and descriptions, i would store them in an array of arrays. that is, something like this: pseudocode.... $all_links = array(); while(looking for links) { if (a link is found) { $all_links[] = array($link_url, $link_description); } } // After you find and store all links and descriptions... foreach ($all_links AS $link_info) { echo $link_info[0]." | ".$link_info[1]; // choose appropriate line break. } Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363442 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 in your example above foreach($match[1] as $url ) && foreach($desc[1] as $fulldesc){ echo urldecode($url); echo "\| $fulldesc<br>"; } you could fix it like this foreach($match[1] as $K => $url ) { $fulldesc = $desc[1][$K]; echo urldecode($url); echo "\| $fulldesc<br>"; } BUT.. if a description is missing it will mess up.. my code will not have this problem, but for the output change this echo "found: $href with the title: $title"; to this echo urldecode($href)." | $title"; Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363451 Share on other sites More sharing options...
kektex Posted October 6, 2007 Author Share Posted October 6, 2007 Thanks MadTechie your fix in my code worked great! I went through your other code but since I dont know that much PHP I had a hard time understanding it.I will go through it later once I get a bit more PHP savvy. Also, thanks BlueSkyIS, I will also take a look at your code when I'm learning about multidimensional arrays. I think I might improve my script later on using those arrays. Quote Link to comment https://forums.phpfreaks.com/topic/72052-solved-accessing-multiple-arrays-using-foreach/#findComment-363461 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.