Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Array Matching

  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Array Matching

    Hello,

    I am pulling data from MySQL into an array, and trying to match an input value against it. This works hit and miss. In some instances, in the arrary will be domain values such as .cn to block all Chinese urls. In other instances, there will values such as testmoney.com, and if a user enters money.test.com it still matches. Anyone have any thoughts on this? Thanks -

    PHP Code:
                $blocked    explode("\n"$row['content']);
                
                foreach(
    $blocked as $value )
                {
                if(
    strpos(strtolower($url), strtolower(trim($value))) !== false
    I've tried in_array, but it doesn't seem to work 100% of the time either.

  • #2
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    What does $row['content'] contain?

    Can you print it here?

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It contains a list of urls and top level domains like this :

    Code:
    .cn
    .ru
    thisurl.com
    thaturl.com
    anotherone.net
    So, the data is taken out, and loaded into the array $blocked, then compared as variable $value to an input the user puts in a form (an url)

    Problem I am having ; it sometimes matches out of order. for example, in the above example - url.that.com matches.

    What I am trying to do is say :

    if the text is in that list, and shows up in an url - it fails.

    So these would fail :

    anything.ru
    thiswontpass.ru.com
    thisurl.com/test/alsodoesntpass/.index.html
    Last edited by sgtc; 03-31-2010 at 05:44 PM.

  • #4
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I think I figured this out. I should be using the strings in the db field without the dots and punctuation. These are causing this routine to work unpredictably.

    Does anyone still have input or experience with periods or reserved characters causing this type of behaviour?

    Edit - and after thinking about this some more, it's probably best to separate the domain level blocking from the strpos blocking - valid urls would be blocked with this method : i.e.

    www.cnn.com
    Last edited by sgtc; 03-31-2010 at 06:11 PM.

  • #5
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    I can't think of any reason why the dots and other punctuation in the URLs would prevent this from working.

    in_array, which you say you tried with inconsistent results, ought to work just fine.

    What would be useful would be if you could paste here an example of the contents of $blocked where in_array does not work. I'll then run the same code and see if I can find what's amiss.

    Go back to the in_array idea, then post your code that contains the in_array here, so I can give it the once-over.

  • Users who have thanked cfructose for this post:

    sgtc (04-01-2010)

  • #6
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I can't think of any reason why the dots and other punctuation in the URLs would prevent this from working.
    You're right - they were not the issue, I just assumed they were when I couldn't get the results I predicted.

    The real issue was that the TLD caused blocking of valid domains. For example -
    blocking .ru, also caused issues with www.running.com - that's just strpos doing it's job.

    I've fixed the issue by using : .blocked-domain.com and using a function to parse_url and get the TLD.

    Thanks for taking out the time to help cfructose - I DO appreciate it!

  • #7
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Most welcome! (Even though I didn't do much.)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •