Solved

PHP check if duplicated found from a TXT file

Posted on 2010-09-12
7
278 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 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
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 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 93
if statement malfunction 5 43
JSON decode 5 43
How to prevent duplicates in dropdown when using dynamic and static data 5 43
This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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