RE: [PHP] converting C to PHP

From: Date: Thu, 13 Jun 2002 04:13:48 +0000
Subject: RE: [PHP] converting C to PHP
Groups: php.general 
Request: Send a blank email to php-general+get-101890@lists.php.net to get a copy of this message
But what about the pointer &cp2 If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr. (Thus, if *nptr is not \0' but **endptr is \0' on return, the entire string is valid.) Instead of a pointer - a position number would be okay.... (Actually, to get the number, I could just do "$cp-0" or "$cp*1" - gotta love variants) -----Original Message----- From: Daniel Tryba [mailto:php-general@canopus.nl] Sent: Thursday, June 13, 2002 2:12 PM To: Martin Towell Cc: php-general@lists.php.net Subject: Re: [PHP] converting C to PHP On Thu, Jun 13, 2002 at 01:12:18PM +1000, Martin Towell wrote: > I'm converting some C functions to PHP and have come across this: > > int skipf(..., char *format) > { > /* ... */ > cp = format; > char *cp, *cp2; > multiplier = strtol(cp, &cp2, 10); > /* ... */ > } > > > How can I implement this line in PHP ? > multiplier = strtol(cp, &cp2, 10); > > > Oh, and an example of format is "12L20W2P" And the outcome in this case would be 12? From man strtol: <quote> The string must begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional +' or -' sign. If base is zero or 16, the string may then include a `0x' prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is `0', in which case it is taken as 8 (octal). The remainder of the string is converted to a long int value in the obvious manner, stopping at the first charac­ ter which is not a valid digit in the given base. (In bases above 10, the letter `A' in either upper or lower case represents 10, `B' represents 11, and so forth, with `Z' representing 35.) </quote> Sounds like a regexp would work, with base 10 this would make: function skipf($str) { if(preg_match("/^\s*(\+|-)?(\d+)/",$str,$match)) { return intval($match[1].$match[2],10); } else { return false; } } $format="012L20W2P"; $int=skipf($format); $int is 12 in this example. -- Daniel Tryba

« previous php.general (#101890) next »