note 107032 deleted from function.is-numeric by googleguy

From: Date: Tue, 22 Oct 2013 06:50:37 +0000
Subject: note 107032 deleted from function.is-numeric by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196672@lists.php.net to get a copy of this message
Note Submitter: holdoffhunger at gmail dot com ---- I wanted an Is_hex function that does the same as is_numeric, except for hex. Here's one with decent efficiency that uses direct iteration. $result stores the result as a TRUE/FALSE string (which can be changed to bool, depending on how you want to implement it), and $string_to_test is the string of possible-hex values you want to test. <?php // Released Under Affero General Public License, Version 3 (AGPL3) // Author: holdoffhunger@gmail.com $result = "TRUE"; $testable_string = strtolower($string_to_test); $testable_string_length = strlen($string_to_test); for($i_string = 0; $i_string < $testable_string_length; $i_string++) { $current_value_to_test = $testable_string[$i_string]; if( ($current_value_to_test != "0") && ($current_value_to_test != "1") && ($current_value_to_test != "2") && ($current_value_to_test != "3") && ($current_value_to_test != "4") && ($current_value_to_test != "5") && ($current_value_to_test != "6") && ($current_value_to_test != "7") && ($current_value_to_test != "8") && ($current_value_to_test != "9") && ($current_value_to_test != "a") && ($current_value_to_test != "b") && ($current_value_to_test != "c") && ($current_value_to_test != "d") && ($current_value_to_test != "e") && ($current_value_to_test != "f") ) { $result = "FALSE"; $i_string = $testable_string_length; } } ?>

« previous php.notes (#196672) next »