Filter non-numeric characters
December 24, 2008
Usually when creating a scraper or a simple parser we encounter data that are usually not as clean as we expected it to be. For example We should be parsing only the sale price but the label gets in the way. These codes could be useful for the purpose of filtering out unwanted characters.
Integers:
$dirty_data = 'You\'re now 26 years old'; $clean_integer = ereg_replace("[^0-9]", "", $dirty_data); // $clean_integer = '26';
Decimals:
$dirty_data = 'Sale price : $12.45'; $clean_dec = ereg_replace("[^0-9.]", "", $dirty_data); // $clean_dec = '12.45';
This would also do good on prefomating a phone number if you only want numbers but your users adds dashes or spaces in between.
Filed under: PHP, PHP & MySQL |
Comments (0)
Related Links:
