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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php