Jump to content

Archived

This topic is now archived and is closed to further replies.

cooldude832

Removing non digits in a string

Recommended Posts

Actually, I kind of made an ass of myself here.

 

preg_replace( '/[^\d]/', '', $str );

 

was faster than my code above.

 

Here's my benchmarks.

 

<?php

set_time_limit( 0 );

$smallStr = 'q2r5uUuT3VIRLvv6Wc7qKhyEtEObfj8oFJdYJxTdvqIgUIwbOahDOh7vF8HFo9FvJ0iJ3SQg57foO5EKtfIA8EMf0xAAH8QCle8d';
$largeStr = 'EqzGNBrw7FxiI6dxgMXEsmzIIZvxlIdTKQkIDFWAQP6L24J2xWXoFB4Y0DrP53gii46Uk71QnrHkAGU161eLp3eDU76XO3fxd6Pkr63hd8ub8ipH4YY3WP4VmyU8YvrlOcLMu26jTPKbtXNs2DuffklajkK3mbne712tc72G2nE7GwvYwmqYCMm2je7Vz8aOAbTD7x6qETHeYM21ZVESPo0IzvndDEogjxerD8NxBDe8huACFkMrRgIwKHUrJ94io7Pq2K3olr0szPmXarSCoUuHxYl0VutIwr6q5vC42qPR2ZI0TMIJSljeAgdmwAN4fcwfi1cOpcLQdE5clLJX2HFvWRJYSREzbhXR64JDs4xjMHijjg7ia2bByRYv042XyL6Q3LDJcspZTZvOkqw5nsSf0XBsCWZeIzdnap5Fnlr4FAR2ramqsBCvtLxnIKvp4SYRko2SpiS1yVhPmLWi7491OCwOdl57zVCgIK2zM3F2EeLef4FFFZ51vzgC0yQTLpx2G0DKeiZbxPHpZyqaOAf2BAjxDxXh8ZWyD8ReU61P7wNjg48Z7VRw53bJ0RiNzP1t5CF1plZ95wpDKuEV2C68U84cTljXyRibU7eMapw6hCo87c333fFI0bANQeKIZ3DYXOMmPKVOUg7OYtZ2y5thb9mMCA6AhMXvqN1jQRYmbVXck7VglSIpdfr11qyqRsm7JwwQyF712V8tgHBgpoyoJsoj7DSBNgtZ872nQDEVUdQAKpG9ml2hdYHALDYFmR4Od7K604SN4PTmDObB4yz6rBYWpNYQKgKrHXiJzERsZ0p71mDIF9op4Brd56PdXnY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'';
for( $i=0,$c=strlen($str); $i<$c; $i++ )
	if ( is_numeric($str[$i]) )
		$num .= $str[$i];
return $num;
}

function rgx ($str) {
return preg_replace( '/[^\d]/', '', $str );
}

function timer( $func, $arg, $loops ) {
$start = microtime(true);
for ( $i=0; $i<$loops; $i++ )
	call_user_func( $func, $arg );
return microtime(true) - $start;
}

echo 'Regex small: ' . timer('rgx', $smallStr, 10000 ) . '<br />';
echo 'String small: ' . timer('str', $smallStr, 10000 ) . '<br /><br />';

echo 'Regex big: ' . timer('rgx', $largeStr, 10000 ) . '<br />';
echo 'String big: ' . timer('str', $largeStr, 10000 ) . '<br /><br />';

?>

 

Outputs:

 

Regex small: 0.72497701644897
String small: 1.4104418754578

Regex big: 17.620409011841
String big: 38.559011936188

 

PHP 5.2.6, Apache 2.2.8

 

But what cooldude832 says still makes no sense.

Share this post


Link to post
Share on other sites

Yeah, discomatt, I was going to say that the for loops would probably be slower.

Share this post


Link to post
Share on other sites

Actually, I kind of made an ass of myself here.

 

preg_replace( '/[^\d]/', '', $str );

 

was faster than my code above.

 

Here's my benchmarks.

 

<?php

set_time_limit( 0 );

$smallStr = 'q2r5uUuT3VIRLvv6Wc7qKhyEtEObfj8oFJdYJxTdvqIgUIwbOahDOh7vF8HFo9FvJ0iJ3SQg57foO5EKtfIA8EMf0xAAH8QCle8d';
$largeStr = 'EqzGNBrw7FxiI6dxgMXEsmzIIZvxlIdTKQkIDFWAQP6L24J2xWXoFB4Y0DrP53gii46Uk71QnrHkAGU161eLp3eDU76XO3fxd6Pkr63hd8ub8ipH4YY3WP4VmyU8YvrlOcLMu26jTPKbtXNs2DuffklajkK3mbne712tc72G2nE7GwvYwmqYCMm2je7Vz8aOAbTD7x6qETHeYM21ZVESPo0IzvndDEogjxerD8NxBDe8huACFkMrRgIwKHUrJ94io7Pq2K3olr0szPmXarSCoUuHxYl0VutIwr6q5vC42qPR2ZI0TMIJSljeAgdmwAN4fcwfi1cOpcLQdE5clLJX2HFvWRJYSREzbhXR64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6jxRE4T7QGPxPL4BjJ5hQg90tzeUT952KT0DWgz7DNZUbC7VIZfpRstc4M1OS072MYdxomjMrfTkqKM6exdHP4dJN0VpXg9zuBHRdpqnFr8o6AqQ79KI2vnHxqxyi0EeNXdJy29wyNMCeqx74q7AKfIK4J7FFgsHjIhoPM4qhDXc70M5WDa0pDgV3Gn4qejUR6zLB298zDJYAn6EIKRjIXNOxIP8BufjR6WRxgc0cAPRFdIlteb6OSdT7tAGDQd88C7rcjGbnTuwBLESdJ4AybAlgPxaZ787jybPrkKByG5frM0XhcvBvls9NpoNX5LI5Gu9KHo2Ad3b6eFlkPBIhmAQelA0X5nsDXUWuCvqRYzR2NK39aWlOFtBLr8RRSbhxfaM7tggEn9SwqRn1RjAubVYoEetM95TopwLx6IkURQB0N81Pg86VGFlU0ZMuC5Q0TOxjV2t2l';

function str ($str) {
$num = '';
for( $i=0,$c=strlen($str); $i<$c; $i++ )
	if ( is_numeric($str[$i]) )
		$num .= $str[$i];
return $num;
}

function rgx ($str) {
return preg_replace( '/[^\d]/', '', $str );
}

function timer( $func, $arg, $loops ) {
$start = microtime(true);
for ( $i=0; $i<$loops; $i++ )
	call_user_func( $func, $arg );
return microtime(true) - $start;
}

echo 'Regex small: ' . timer('rgx', $smallStr, 10000 ) . '<br />';
echo 'String small: ' . timer('str', $smallStr, 10000 ) . '<br /><br />';

echo 'Regex big: ' . timer('rgx', $largeStr, 10000 ) . '<br />';
echo 'String big: ' . timer('str', $largeStr, 10000 ) . '<br /><br />';

?>

 

Outputs:

 

Regex small: 0.72497701644897
String small: 1.4104418754578

Regex big: 17.620409011841
String big: 38.559011936188

 

Try using [\D] instead of [^\d]. I'm wondering if it will be slightly faster by excluding the "not" operator (^).

Share this post


Link to post
Share on other sites

Hmm, 2008 called... it wants its thread back!

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.