Jump to content

[SOLVED] Accessing multiple arrays using foreach


kektex

Recommended Posts

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  :P

 

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.

 

 

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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).

 

Link to comment
Share on other sites

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.
}

Link to comment
Share on other sites

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";

 

Link to comment
Share on other sites

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.

 

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.