PHP 正则表达式拆分发票行项目描述



我正在尝试像下面这样拆分字符串:

一个项目 ( Item A ) 可能包含 89798 个数字和字母 @ $550.00

第 4 Item B @ $420.00

Item C 476584,数量较大,货币符号不同 @ £420.00

到:

array(
    0 => 1
    1 => "some item which may contain 89798 numbers and letters"
    2 => $550.00
);

这有意义吗?

我正在寻找一种正则表达式模式,它将拆分数量、描述和价格(包括符号)。

字符串将始终为:

qty x description @ price+symbol

所以我假设正则表达式是这样的:

`(match a number and only a number) x (get description letters and numbers before the @ symbol) @ (match the currency symbol and price)`

我应该如何处理这个问题?

preg_match('/^(d+)s*xs(.+)s@s*([^@]+)$/', $string, $match);
unset($match[0]);
print_r($match);

如果x@两侧总是有空格 你可以试试这个:

$pattern = '/s*(x|@)s*/';
$array = preg_split($pattern, $input_string);

最新更新