Solved

PHP check if duplicated found from a TXT file

Posted on 2010-09-12
7
273 Views
Last Modified: 2012-05-10
Hi experts...
as mentioned on the title, I want to check if duplicated (URL) found from a TXT file.

For example here is the contents of the TXT file (list 1 URL in each line):
adobe.com
corel.com
ebay.com
paypal.com
and some many more ...

And here's my PHP codes which surely looks silly to you  :(

$forbiddenURL = file_get_contents('test.txt');
$testURL = 'http://www.adobe.com/optional/dir/might/presents/here/';
if ( $testURL == $forbiddenURL ) {
      echo 'URL is found on Blocked list';
} else {
      echo 'URL is not found on Blocked list';
}

But it's not working so I need working codes ... BTW the TXT file might content up to 100 lines of URL and I think the use of MySQL DB is not necessary for me to easier update TXT file if needed.

Looking forward for a solution here .... Thanks all.
0
Comment
Question by:trrsrr
7 Comments
 
LVL 1

Expert Comment

by:ahmad_alinat
ID: 33657486
you can load the text into a string using file_get_contents
then exlode the string by \n using the explode function

then use array_unique function to get the urls without duplicates
then compare the two arrays (original and unique) using count function

http://php.net/manual/en/function.count.php
http://php.net/manual/en/function.array-unique.php
http://php.net/manual/en/function.explode.php
0
 
LVL 12

Assisted Solution

by:zappafan2k2
zappafan2k2 earned 250 total points
ID: 33657488
Try this
$forbiddenURLs = file('test.txt');

$testURL = 'http://www.adobe.com/optional/dir/might/presents/here/';



foreach ($forbiddenURLs as $domain) {

  $domain = trim($domain);    // strip trailing white spaces

  if (preg_match("/$domain/", $testURL)) {

    echo 'URL is found on Blocked list';

    }

  else {

    echo 'URL is not found on Blocked list';

    }

  }

Open in new window

0
 
LVL 1

Author Comment

by:trrsrr
ID: 33657512
@ahmad_alinat:
actually I'm looking for a quick and correct solution ... I've tried your suggestion but I'm stucked even at the 'explode' part of the TXT file  :(

@zappafan2k2:
Your solution gave me a repeated message :
URL is found on Blocked listURL is not found on Blocked listURL is not found on Blocked listURL is not found on Blocked listURL is not found on Blocked listURL is not found on Blocked list....
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33657547
@zappafan2k2's code works as you posted though it may not be what you wanted.  Did you only want one message to tell you whether the $testURL contained on of the domains in the list?
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 33657565
Based on @zappafan2k2's code, this will give you only one message.  Save as 'test.php'.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>URL File Check</title>
</head>
<body>
<h1>URL File Check</h1>
<?php 
$forbiddenURLs = file('urltest.txt');
$testURL = 'http://www.adobe.com/optional/dir/might/presents/here/';
$count = 0;
$domain = "";
foreach ($forbiddenURLs as $domain) {
  $domain = trim($domain);    // strip trailing white spaces
  if (preg_match("/$domain/", $testURL)) {
		$count += 1;
		break;
		}
  }

  if ($count) {
    echo "$domain :: URL is found on Blocked list<br>";
    }
  else {
    echo "URL is Not found on Blocked list<br>";
    }
	
?>
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:trrsrr
ID: 33657571
@zappafan2k2 & @DaveBaldwin:
actually (just checked) @zappafan2k2's code is not working precisely as it will only check for the top URL on the TXT file which is adobe.com

I tried ebay.com and paypal.com and the result is valid...
0
 
LVL 1

Author Closing Comment

by:trrsrr
ID: 33657579
Ok thanks for working codes ... points splitted.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Download a website to hdd 2 52
designing in object programming 12 52
php function to remove a file 30 18
paypal ipn to mysql 3 6
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now