0

i'm trying to formulate a regular expression to find prices in a string and put html tags around. i've come up with this :

    $pattern = '/([1-9][0-9]*[,|.][0-9]{2})/i';
    $replace = "<p>$1</p>";

    $string = preg_replace($pattern, $replace, $string);

this works well so far for most cases except when something like 22,123 appears which will also be changed to

    <p>22,12</p>3

how can i make this reg ex ignore numbers with more than two decimals?

4

1 回答 1

0

您可以在其末尾添加一个否定的前瞻

$pattern = '/([1-9][0-9]*[,|.][0-9]{2})(?!\d)/i';

这会阻止数字在匹配后出现。(顺便说一下,所有的[0-9]s 都可以变成\ds。)

有用!

于 2013-05-05T14:02:00.873 回答