11c04-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 27 行
PHP
27 行
<?php// A function that will accept and clean up number stringsfunction standardize_number($num, $precision = false) { // First, remove all non-digits, periods, and - signs from the string $num = preg_replace('/[^-.0-9]/', '', $num); // Now remove any -'s that are in the middle of the string: $num = preg_replace('/(?<=.)-/', '', $num); // We now have a valid string that PHP will properly consider a number. // If a precision was asked for, round accordingly: if ($precision !== false) { $num = round($num, $precision); } return $num;}// Standardize some number strings:$nums = array('123.4643', 'Hello I bought 42 flowers for you.', '-344-345.424', '+544,342.566');foreach ($nums as $num) { $st = standardize_number($num, 2); echo "<p>{$num} = {$st}</p>\n";}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?