veloxia/php-helpers

一些PHP辅助函数。

v1.2 2020-03-10 19:29 UTC

This package is auto-updated.

Last update: 2024-09-11 14:48:32 UTC


README

Latest Stable Version Total Downloads Build Status

一些PHP辅助函数。

安装

composer require veloxia/php-helpers

使用

捕获

使用 capture() 可以更轻松地返回正则表达式的 [1] 组,如果没有设置括号则返回 [0]。函数会自动设置分隔符。

$text = 'This costs $200,00 including shipping.';

$exp = '\$(200),00'; // instead of /\$(200),00/i

echo capture($exp, $text);  // returns 200

还可以使用 capture_list()。在这种情况下,将返回表达式列表中的第一个匹配项。

$text = 'This costs 200 EUR including shipping.';
$exps = [
  '(\d+) USD',
  '(\d+) GBP',
  '\$(\d+)',
  '(\d+)',
];
echo capture_list($exps, $text); // => 200

数字范围

number_range 可以动态创建一个数值范围。示例

echo number_range(10.5, 13.9, 2, '%');
// 10,50 – 13,90 %