Re: [PHP] comma-formatting numbers

2001-11-17 Thread Paul Wolstenholme

You can do this using the number_format function or probably sprintf.
/Paul

On Saturday, November 17, 2001, at 01:36  PM, Scott Dudley wrote:


 i'm new to php and am having difficulty translating this tiny awk
 function that i use to comma format numbers.  can someone assist?  my
 stumbling block thus far have been the fact the the php regex matching
 functions don't return the byte offset within the haystack and the regex
 replace functions don't support the awk and perl-like ampersand  in
 the replacement pattern.  please help and thanks.

 function commas(num) {
   while (num ~ /[0-9][0-9][0-9][0-9]/)
 sub (/[0-9][0-9][0-9]$|[0-9][0-9][0-9][,]/, ,, num)

   return num
 }


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Check if ip is with a network block

2001-11-14 Thread Paul Wolstenholme

Greetings,

I'd like to check to see if an ip is within an ip cidr style network
block (192.75.242.157 with 192.75.242.0/23). Initially, I started off
with some code derived from some perl code that I found (code below).
However, the bitmask calcution did not work ( $bitmask = 0x 
(32 - $nbits);) and the hexdec did not appear to support values as bit
as  0x. 

So I did a bit more research and found a relevant posting on the php
mailing list. If the function 'within' works properly it should return
true if the ip is within the network block but I cannot seem to get it
to work properly. Are there any CIDR/bitwise operator wiz's out there
that could help me get this straightened out. Thanks.

/Paul

Input:
remote_ip = 192.75.242.88
cidr = 192.75.242.0/24 

Output:
ip = -1068764584 
start = -1068764672 
mask = -16777216 


function within ($remote_ip, $cidr) {
## within function checks whether an ip is within a network
block
## example with(192.75.242.157, 192.75.242.0/24) 
## returns true if ip is within range

$ip=ip2long($remote_ip); 
list ($quad, $nbits) = split (/, $cidr, 2);
$shift_mask = (integer) $nbits; 
$start=ip2long($quad);

$mask = -1$shift_mask;

return $ip  $start == $mask;
}




PERL derived code:
$cidr = $valid_client; /* db value 192.55.192.0/24 */
list ($quad, $nbits) = split (/, $cidr, 2);
$byte = explode (., $quad);
$address_required = ($byte[0]  24) | ($byte[1]  16) | ($byte[2] 
8) | $byte[3];

/* remote host info */
$byte = explode(., $ip); /* ip from $HTTP_SERVER_VARS['REMOTE_ADDR']
*/
$address_client = ($byte[0]  24) | ($byte[1]  16) | ($byte[2]  8)
| $byte[3];

$bitmask = 0x  (32 - $nbits); 
if (($address_client  $bitmask) == ($address_required  $bitmask)) {
echo Ip is within block;
} else {
echo Ip is NOT within block;
}
-- 

Paul Wolstenholme
SMA Webware
http://www.zzube.com/
What do you know?
http://make.zzube.com/
Vancouver, BC Canada


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]