Why doesn’t regular expression /£?.*£?/ match string ‘£45.56–34.90’ in PHP 8.2?

  Kiến thức lập trình

I’m using PHP 8.2.11 on Mac Sonoma 14.5.

My code is:

$cost = '£45.56–34.90';
$matches = array();
$found = preg_match(
  '/£?.*£?/',
  $cost,
  $matches
);

I’d expect preg_match() to return 1, indicating a match, and to populate $matches like this:

array (
  0 => '£45.56–34.90',
)

However, $found === 0 and $matches is empty.

I tested this on https://regex101.com, which matched as I expected.

If I change my code so that $cost === '£45.56–£34.90' (with a second pound sign added), then $found === 1 and $matches is populated as:

array (
  0 => '£45.56–£34.90',
)

If I change my code so that $cost === '£45.56–34.90' and the pattern is '/£?.*£?/g', then $found === false.

New contributor

pandammonium is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT