...

View Full Version : Array Matching



sgtc
03-31-2010, 01:34 AM
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 -



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

cfructose
03-31-2010, 08:29 AM
What does $row['content'] contain?

Can you print it here?

sgtc
03-31-2010, 04:41 PM
It contains a list of urls and top level domains like this :



.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

sgtc
03-31-2010, 05:04 PM
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

cfructose
03-31-2010, 11:46 PM
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.

sgtc
04-01-2010, 01:52 AM
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! :thumbsup:

cfructose
04-01-2010, 10:52 AM
Most welcome! (Even though I didn't do much.) ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum