Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FCNahian's avatar

How to use PHP preg_replace on a formatted number based on search query while ignoring comma and decimal?

I want to highlight my search query on a formatted number.

For example:

$search_query = '1234'; // or $search_query = '7800';
$formatted_numeber = '12,345,678.00';

Currently my code is:

preg_replace('/(' . $search_query . ')/i', "<span style='background: yellow'>$1</span>", $formatted_numeber);

But this code fails to highlight search clause like '1234' , '7800' due to presence of commas (,) and decimal (.)

Also i want this to work with the presence of commas and decimals in the search query.

For example:

$search_query = '3,456';
$formatted_numeber = '12,345,678.00';

I want to highlight the part "345,6" on my formatted number.

Or:

$search_query = '7.80';
$formatted_numeber = '12,345,678.00';

I want to highlight the part "78.0" on my formatted number.

0 likes
1 reply
FCNahian's avatar
FCNahian
OP
Best Answer
Level 2

Got the solution on stackoverflow and tweaked it a bit for a bug.

Reference: https://stackoverflow.com/questions/78959860/how-to-use-php-preg-replace-on-a-formatted-number-based-on-search-query-while-ig

1 like

Please or to participate in this conversation.