Array comparing problem

Discussion in 'PHP' started by is0is0, Aug 7, 2008.

  1. #1
    Greetings!

    Sorry if anyone disagrees with the fact that I posted this on another forum and I also apologize for not introducing myself properly, I will remove this post if nobody agrees with the fashion it is presented in, but any help would be greatly appreciated!

    On with the problem!

    Ok so I have good friend in the same industry as I am, he wanted to trade links with me, so is said sure, so he gave me a big list of links that he had so that I could put it in my directory, and I have a txt file with a list of all my links from my site in it, basically what I want to do is make a PHP script that compares both lists of links and return an array(list) the links that he has and I don't, basically I don't want duplicates, I don't want to put one of his links in and discover later that I already had it! Now I've made a little script like this before and I simply used array_diff() to find the difference, now the problem with my current situation is that I need the category from his links so I know where to put mine, here's an example:

    My list:
    http://www.blahblah.com/blablah/blah.html
    http://www.blahblah.com/blablah/blah.html
    http://www.blahblah.com/blablah/blah.html

    His list:
    <li><a href="http://blah.com/tutorials/lol.html">Tutorials</a>

    <li><a href="http://www.blablah.com/cars/cars.html">Cars</a>

    <li><a href="http://www.blahsasdasd.com/weather/weather.html">Weather Information</a>


    Now the task here is to compare both of these lists together to strip out the links that I already have in my link file and create a new text file with all the links in his file that I don't have, BUT the challenge is to KEEP the link structure INTACT, I NEED an WANT those tags in that output file, but I have no idea how to compare both files without stripping out the tags and categorys!

    If anyone could help it would be much appreciated, thanks!
    is0
     
    is0is0, Aug 7, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First, make an array from your list,
    $my_array = explode("\n",$mylist);
    PHP:
    Next, use a regular expression to process his list into an array.
    preg_match_all('/<li><a href="(.*?)">(.*?)</a>/',$hislist,$his_array);
    PHP:
    Then, go through each of his links, and check if you have them
    
    foreach($his_list[1] as $k=>$v) {
     if(in_array($v,$my_array)) {
          print $his_list[0][$k];
    }
    }
    
    PHP:
     
    matthewrobertbell, Aug 7, 2008 IP
  3. B-Scan

    B-Scan Member

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Yep, matthewrobertbell was right - just one simple error to correct...
    In foreach is $his_array[1] - not $his_list[1].
     
    B-Scan, Aug 7, 2008 IP
  4. is0is0

    is0is0 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks alot, I appreciate it, it got me started off on the right foot, however, I tried it out with fwriting the result to a file rather than printing it and it did not work, the file came out blank, my guess is becuase the function in_array compares the entire value to another entire value so basically it was comparing these two:

    <li><a href="http://www.blah.com/sasdad/adssad.php"></a>
    http://:www.blah.com/sasdad/adssad.php

    They did not match becuase one has the HTML tags and one does not, my guess is that you were under the impression that in_array tries to search the value for anything similar to the match you specified when it does not, it matches the entire value to the other value, so they don't match.

    I could be wrong, but that's what happened.
    Thanks again for your help, if you can elaborate on this it would be well appreciated.
     
    is0is0, Aug 7, 2008 IP
  5. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you print_r($his_array); , you should see that it contins three other arrays, with [1] being an array of links without the html. That's what i was aiming for it to compare. Does it print them out successfully?
     
    matthewrobertbell, Aug 8, 2008 IP
  6. is0is0

    is0is0 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I printed $his_array and I got One array with the first key being another array containing all the links with the HTML, like this,

    Array
    (
    [0]=>Array
    (
    [0]=>LINK
    [1]=>LINK



    And so on...
     
    is0is0, Aug 8, 2008 IP
  7. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #7
    There should be more than one array within $his_array, is there not?
     
    matthewrobertbell, Aug 9, 2008 IP
  8. is0is0

    is0is0 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No, there's only one array inside $his_array.
     
    is0is0, Aug 9, 2008 IP