?
Solved

PHP check if duplicated found from a TXT file

Posted on 2010-09-12
7
Medium Priority
?
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

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 84

Accepted Solution

by:
Dave Baldwin earned 1000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 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