Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Separate list of URLs on a text file

Status
Not open for further replies.

parazite

Technical User
Feb 1, 2011
2
CA
I have a text file containing a list of URLs like this one:

...

I would like to find a way to separate those that contain a keyword, here let's say "hotel albany" by those that do not contain the keyword and the result to be saved on two separate files, one with good results, one with the rest of the results. Is this possible?
 
Yes, open the file, and loop through the contents search for your keywords if found store url in an array, then write the files out.


Code:
$filename="urllist.inc";
$keywords=array('hotel','albany');
$urls=file($filename);
$goodurls=array();
$badurls=array();
foreach($urls as $url){
if(strpos($url,$keywords[0]) || strpos($url,$keywords[1])){
$goodurls[]=$url;
}
else{
$badurls[]=$url;
}
}

echo "Good Urls:<pre>";
print_r($goodurls);
echo "</pre><hr>";


echo "Bad Urls:<pre>";
print_r($badurls);
echo "</pre><hr>";
?>


then its just a question of writing out the files with fopen() and fwrite() functions.

This takes any of the urls with at least one of the keywords as a good url. If both need to exist in the url, change || for && in th if statement.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top