HI,

All the exmaples i've come across on the internet either show you how to create or echo arrays, given that you enter the values yourself. But I need to choose a value from within an array that has 560, and I don't know the value.

So far my script looks for URLs within a html page then inserts them all into one array. When I:

$result = count($matches, COUNT_RECURSIVE);
echo $result;

it displays " 560 ". So how can I choose the 560th value from within the array, so I can use it? I tried $match[560], didn't work :/ Not too good on arrays. Could someone point me in the right direction please?

thanks

    If the array is numerically indexed, use $array[559]

    If it is an associative array, you can use [man]array_values/man to get the numerically indexed version, or you could loop through the array with a foreach loop until the 560th element is reached.

      Ok, I'm getting some where now lol, but ... It's very confusing now :/

      I did as you said:

      print_r(array_values($match));

      And it only prints out 3 values :S

      Array ( [0] => eBay official time [1] => " [2] => http://cgi1.ebay.co.uk/aw-cgi/eBayISAPI.dll?TimeShow&ssPageName=f:f:UK [3] => eBay official time ) 

      And it said it had 560 :/ 😕

      Btw, if you're wondering why ebay's showing up, I'm just testing it for all its links 😛

      edit:\ Ah nevermind, I meant to print 'matches' not 'match'. Thanks, I'll post back when I get further.

      edit:\ Ok another question now. When I print all the array's values, they repeat 😕

      [1] => " [2] => # [3] => Continue ) [22] => Array ( [0] => Cancel and don\'t ask me again on this site. [1] => " [2] => # [3] => Cancel and don\'t ask me again on this site. ) [23] => Array ( [0] =>  [1] => " [2] => http://ebay.doubleclick.net/jump/ebay.uk.homepagevisitor/visitor;tile=9;sz=326x44;ord=123456789? [3] =>  ) [24] => Array ( [0] => Click here [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=13 [3] => Click here ) [25] => Array ( [0] => Automotive [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=17 [3] => Automotive ) [26] => Array ( [0] => Books, Comics & Magazines [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=18 [3] => Books, Comics & Magazines ) [27] => Array ( [0] => Business, Office & Industrial [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=89 [3] => Business, Office & Industrial ) [28] => Array ( [0] => Clothes, Shoes, Accessories [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=19 [3] => Clothes, Shoes, Accessories ) [29] => Array ( [0] => Collectables [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=20 [3] => Collectables ) [30] => Array ( [0] => Computing [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=21 [3] => Computing ) [31] => Array ( [0] => Consumer Electronics [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=22 [3] => Consumer Electronics ) [32] => Array ( [0] => DVD, Film & TV [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=23 [3] => DVD, Film & TV ) [33] => Array ( [0] => Health & Beauty [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=88 [3] => Health & Beauty ) [34] => Array ( [0] => Home & Garden [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=24 [3] => Home & Garden ) [35] => Array ( [0] => Jewellery & Watches [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=25 [3] => Jewellery & Watches ) [36] => Array ( [0] => Mobile & Home Phones [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=26 [3] => Mobile & Home Phones ) [37] => Array ( [0] => Music [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=27 [3] => Music ) [38] => Array ( [0] => Musical Instruments [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=28 [3] => Musical Instruments ) [39] => Array ( [0] => PC & Video Gaming [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=29 [3] => PC & Video Gaming ) [40] => Array ( [0] => Photography [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=30 [3] => Photography ) [41] => Array ( [0] => Sporting Goods [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=31 [3] => Sporting Goods ) [42] => Array ( [0] => Tickets & Travel [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=33 [3] => Tickets & Travel ) [43] => Array ( [0] => Toys & Games [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=34 [3] => Toys & Games ) [44] => Array ( [0] => All Categories... [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=35 [3] => All Categories... ) [45] => Array ( [0] => All items ending now! [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=36 [3] => All items ending now! ) [46] => Array ( [0] => Brand New items [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=38 [3] => Brand New items ) [47] => Array ( [0] => Items from 99p with no reserve [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=37 [3] => Items from 99p with no reserve ) [48] => Array ( [0] => a|A Change text size [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-7?id=5 [3] => a|A Change text size ) [49] => Array ( [0] => Global Trade [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-7?id=1 [3] => Global Trade ) [50] => Array ( [0] =>  [1] => " [2] => http://ebay.doubleclick.net/jump/ebay.uk.homepagevisitor/visitor;tile=1;sz=275x300;ord=123456789? [3] =>  ) [51] => Array ( [0] => Camera mobiles ending now [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=39 [3] => Camera mobiles ending now ) [52] => Array ( [0] => PS2 - no bids, ending soon [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-7?id=3 [3] => PS2 - no bids, ending soon ) [53] => Array ( [0] => All cars from £200, no reserve [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=40 [3] => All cars from £200, no reserve ) [54] => Array ( [0] => Designer clothes at great prices [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=41 [3] => Designer clothes at great prices ) [55] => Array ( [0] => Handbags - no bids, ending soon [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-7?id=4 [3] => Handbags - no bids, ending soon ) [56] => Array ( [0] => New laptops with warranty [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-14990-3840-5?id=42 [3] => New laptops with warranty ) [57] => Array ( [0] => DVDs - bid from 99p [1] => " [2] => http://altfarm.mediaplex.com/ad/ck/1116-

        hmm... okay, your use of COUNT_RECURSIVE with count() means that you are counting a multidimensional array? Now this changes how you look at what is the 560th element.

        Not only that, but a glance at your print_r() output shows that not all the elements in $match are URIs. You need to provide more information, perhaps the smallest and simplest script that demonstrates your problem.

          I think it shows pictures and stuff, because my criteria I set is to look for links with the <a href=" tag, so image links are showing up.

            BRUm wrote:

            I think it shows pictures and stuff, because my criteria I set is to look for links with the <a href=" tag, so image links are showing up.

            I see.

            Well, put it another way. What is the format of the array that you expect to process?

              I have no idea. When you mean format do you mean like strings and boolen? I'll show you part of my script:

              ?><br><br>
              <br><br><?php
              $result = count($matches, COUNT_RECURSIVE);
              echo $result;
              $value = rand(1, $result);
              ?><br><br><?php
              print_r(array_values($matches));
              $input = file_get_contents("$matches[$value]");
                $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
                if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
                  foreach($matches as $match) {
                    echo $matches[$value]; 
                    #echo $match[3]; 
                  }
                }

              I'll eventually loop it too. What I'm intending it to do, is look through a URL I specify to start it off, then it will look within the html and pick put all the links into an array, then it will pick a random value from the array, and will search within the URL the value is. The idea is a basic web crawler for my search engine. Hm.

                When you mean format do you mean like strings and boolen?

                No, I mean how does $matches look like. You want to select one element in $matches, but you havent stated if $matches is an associative array or a numerically indexed array. It looks like $matches is a multi-dimensional array, but you havent said anything about that either.

                What you have done is provide the output of:
                print_r(array_values($matches));

                But then you say that there's some problem with the output. If I dont know what the expected result is supposed to be, I'm working blind, and might as well not bother helping you since my ideas wont work.

                  Does the above code help you?

                  edit:\ Ok this is all the code:

                  input = file_get_contents("http://www.ebay.co.uk");
                    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
                    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
                      foreach($matches as $match) {
                        ?>
                        <br><br>
                        <?php
                        echo $match[2]; 
                        #echo $match[3]; 
                      }
                    #New one
                  ?><br><br>
                  <?php
                  $input2 = file_get_contents("http://www.ebay.co.uk");
                    $regexp2 = "<META NAME=[^>]*CONTENT=(\"??)([^\">]*?)\\1[^>]*>";
                    if(preg_match_all("/$regexp2/siU", $input2, $matches2, PREG_SET_ORDER)) {
                      foreach($matches2 as $matchh) {
                        echo $matchh[2]; 
                        #echo $match[3]; 
                         #$query = "INSERT INTO nero VALUES ('Ebay','$match[2]','$matchh[2]')";
                      mysql_query($query);
                      }
                    }
                  
                  
                   }
                  
                  
                  {
                  ?><br><br>
                  <br><br><?php
                  $result = count($matches, COUNT_RECURSIVE);
                  echo $result;
                  $value = rand(1, $result);
                  ?><br><br><?php
                  print_r(array_values($matches));
                  $input = file_get_contents("$matches[$value]");
                    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
                    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
                      foreach($matches as $match) {
                        echo $matches[$value]; 
                        #echo $match[3]; 
                      }
                    }
                  $i = $i + 1;
                  }
                  ?>

                    Oh, okay. What you have is the array of matches from preg_match_all(). You should not have used COUNT_RECURSIVE, since you dont want to recursively count the array. Your count of 560 is a gross overcount.

                    Now, $matches[0] is the first match. $matches[0][0] contains the entire expression matched by the pattern, while $matches[0][1] contains the expression matched by the first subpattern.

                    $matches[1] is the second match.

                    What you want to do is to select a match at random, and then use it. This is easy:

                    $key = array_rand($matches);
                    echo $matches[$key][0];

                    Ok this is all the code

                    Next time provide the smallest and simplest code that demonstrates your problem, not all the code, including junk that makes it difficult to see what is the problem. You should also document your code properly.

                      ok ok, thanks for your advice. I'm going to start over for a 3rd time. Would you be able to spare some time to help me? It would be greatly appreciated.

                      I was tearing my hair out over this:

                      <?php
                      
                      $username="*";
                      $password="*";
                      $database="*_";
                      
                      mysql_connect(localhost,$username,$password);
                      
                      @mysql_select_db($database) or die( "Unable to select database");
                      
                      {
                      
                      #Start off by giving a website to start the process
                      
                      $urlcontents = File_get_contents( "http://monsterlinkswap.com/" );
                      
                      #Now grab all the links in the page
                      
                      $urls = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
                      
                      if(preg_match_all("/$urls/siU", $urlcontents, $matches, PREG_SET_ORDER )) {
                          foreach($matches as $match) {
                            ?><br><br><?php
                      
                      }
                      $count = count($matches);
                           $val=rand(1, $count);
                           echo "<pre>";
                           print_r( $matches[$val] ); 
                      }
                      while($i < 100)
                      {
                      #now next level
                      
                      $val2 = rand(1, $count);
                      $newurl = $matches[$val2][2];
                      $keywords = $matches[$val][3];
                      $urlcontents = File_get_contents( $newurl );
                      
                      if(preg_match_all("/$urls/siU", $urlcontents, $matches, PREG_SET_ORDER )) {
                          foreach($matches as $match) {
                            ?><br><br><?php
                      
                         $count = count($matches);
                               $val=rand(1, $count);
                               echo "<pre>";
                               print_r( $matches[$val] );
                      
                      
                      
                      }
                      
                          $query1 = "SELECT * FROM nero";
                          $rezult=mysql_query( $query1 );
                          $result_array=mysql_fetch_array($rezult);
                      
                          $exist=$result_array['website'];
                      
                      
                          if ($exist == $newurl)
                          {
                          ?>
                          <html>
                      <meta http-equiv="REFRESH" content="0;url=/grab.php">
                      </html>
                      <?php
                      exit;
                              }
                              $query = "INSERT INTO nero VALUES ('$keywords','$newurl','$keywords')";
                          mysql_query($query);
                      }
                      $i = $i + 1;
                      }
                      }
                      ?>

                      The code doesn't seem much, nut after hours of debugging etc, I have no idea why the hell www.xwebhost.co.uk/grab.php when displaying all the links for my search engine, only adds a few and a lot of them to my database :glare:

                        Write a Reply...