[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

PHP check if duplicated found from a TXT file

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
trrsrr
Asked:
trrsrr
2 Solutions
 
ahmad_alinatCommented:
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
 
zappafan2k2Commented:
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
 
trrsrrAuthor Commented:
@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
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!

 
Dave BaldwinFixer of ProblemsCommented:
@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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
trrsrrAuthor Commented:
@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
 
trrsrrAuthor Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now