Solved

PHP check if duplicated found from a TXT file

Posted on 2010-09-12
7
272 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

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 82

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to count occurrences of each item in an array.
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…

757 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

18 Experts available now in Live!

Get 1:1 Help Now