views:

82

answers:

1

I need the best way of finding how many numbers in a string, would I have to first remove everything but numbers and then strlen?

Also how would I go about testing the performance of a any PHP script I have written, say for speed and performance under certain conditions?

UPDATE

say I had to inlcude ½ half numbers, its definetly preg then is it not?

+2  A: 

You can just split the string with preg_split() and count the parts:

$digits = count(preg_split("/\d/", $str))-1;
$numbers = count(preg_split("/\d+/", $str))-1;

For performance you could use microtime().

eg:

For execution time:

$start = microtime(true);
// do stuff
$end = microtime(true)-$start;

For memory usage:

$start = memory_get_usage(true);
// do stuff
$end = memory_get_usage(true) - $start;

http://us.php.net/manual/en/function.memory-get-usage.php

For peak memory:

memory_get_peak_usage(true);

http://us.php.net/manual/en/function.memory-get-peak-usage.php

There are also specific PHP profiling tools like XDebug. There is a good tutorial on using it with Eclipse: http://devzone.zend.com/article/2930

There is also benchmark in PEAR: http://pear.php.net/package/Benchmark/docs/latest/Benchmark/Benchmark%5FProfiler.html

And a list of others: http://onwebdevelopment.blogspot.com/2008/06/php-code-performance-profiling-on.html

bucabay