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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strings 1

Status
Not open for further replies.

ruler1

MIS
Feb 19, 2007
89
hi peeps, i was wondering if someone could help me.
im having difficulties extracting items from a text file and adding them to variables. the contents of the file below might give an idea of what i am trying to do...

this is a regular visitor:24.8.0.1-24.8.0.255
unknown visitor:124.80.0.1-124.80.0.255
top member:10.90.0.1-10.90.0.255
admin member:22.28.0.255-22.28.0.255
moderator member:22.27.17.11-22.27.17.11

i am trying to put each into a variable when reading through the file for example using something like below..

Code:
$file = fopen("c:users.txt", "r") or exit("Unable to open file!");
while(!feof($file))
  {
    // (put info into $info):(put start IP1 into $ip1).(put start IP2 into $ip2).(put start IP3 into $ip3)....
  }
fclose($file);

any idea's would be great because i just cant think of any way to do this. thank you in advanced
 
this comparison
Code:
if(ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['startIP'][$cc],$regs) && ereg("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})", $matches['endIP'][$cc],$regs1))
is already done within my code. my code will only return addresses meeting these criteria.
to split the ip address up into segments to perform the math use explode(). try using this code for the address comparison

Code:
<?php
/**
 * function for determining whether an ip address is in range
 * 
 * @return bool TRUE if $current is within a range, otherwise FALSE
 * @param $start dotted notation ip address
 * @param $end dotted notation ip address
 * @param $current dotted notation ip address
 */
function compareIP($start, $end, $current){
	$start = iptoDec($start);
	$end = iptoDec($end);
	$current = iptoDec($current);
	if (!$start || !$end || !$current){
		//assume spoof if badly formed IP address and return a no-match
		return false;
	}
	if ($start > $end){
		$s = $end; 
		$end = $start;
		$start = $s;
	}
	return (($current >= $start) && ($current <= $end));
}
/**
 * helper function for compareIP
 * 
 * this function takes dotted notation ip addresses and converts them to decimal
 * it does this by first converting each octet to binary
 * then concatenating the binary and finally converting the binary string to decimal
 * 
 * @return decimal value ip address
 * @param $ip dotted notation ip address
 */
function iptoDec($ip){
	$bits = explode ('.', $ip);
	if (count($bits) !== 4) return false;
	$bin = '';
	foreach ($bits as $bit){
		$bin .= decbin($bit);
	}
	return bindec($bin);
}
?>

i have not tested it, but it looks ok.


but the code you have posted does not deal with adding or deleting IP addresses - i'd just open the text file and do things manually.
 
hi sorry for not replying sooner, i was away for a few days.
i shall try that code later today. i think to call that function i'd have to use something like $temp = compareIP($start, $end, $current) but not 100% sure, i'll figure it out.

i did look on google for ways of deleting lines from text files using php and i was stunned at how many other people were asking the same question. I was more stunned to find that very few were ever solved and those that were solved required large code just to delete a single line. You'd think in this day and age there would be a simple way of deleting a line with php.

 
umm ... it's pretty simply to delete a line.
Code:
$file = 'file.txt';
$contents = file ($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$toKeep = array();
foreach ($contents as $line){
 if ($line == 'something i want to keep'){
  $toKeep[] = $line;
 }
}
writeToFile($toKeep);

function writeToFile($toKeep){
  global $file;
  $w = file_put_contents($file, implode("\n", $toKeep));
  if ($w === false) { return false; } else {return true;}
}
 
there is a fatal error with the line

Code:
writeToFile($toKeep)

it doesnt turn blue in the editor so i looked on and found no topics relating to this function. im not sure if its a typo but the rest seems to be ok
 
there is no error associated with that line. there must be something else wrong.
 
its ok it was my mistake, i placed the function within a foreach loop but when i moved it out of the loop it worked.
i think this topic is now solved as everythig seems to be working tip-top but i couldnt have done it without your help. thank you for the help i am very greatful
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top