02c01-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 31 行
PHP
31 行
<?php// A function to retrieve the number from a string no matter what.function retrieve_number($str) { // We need to do some cleanup of the string // so remove anything that is not a digit, a period, or a - $str = preg_replace('/[^0-9.-]/', '', $str); // We can safely call floatval to convert this to a float and return return floatval($str);}// Setup our test cases:$test1 = '$123,432.55';$test2 = ' a -57.2';// Echo out these if just directly used as a float ...// Displays the following:// test1 = 0// test2 = 0echo '<pre>';echo "\ntest1 = " . ($test1 * 1.0);echo "\ntest2 = " . ($test2 * 1.0);// Now use our new function:// Displays the following:// test1 = 123432.55// test2 = -57.2echo "\ntest1 = " . retrieve_number($test1);echo "\ntest2 = " . retrieve_number($test2);echo '</pre>';?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?