❓ Help Replace function in Javascript equivalent to PHP

jay41

Forum Veteran
What is the equivalent php code of this javascript code.
data.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1.");
 
The equivalent PHP code for the given JavaScript code would be:

PHP:
$data = preg_replace('/(\d)(?=(\d\d\d)+(?!\d))/', '$1.', $data);

In this PHP code, preg_replace function is used to perform a regular E×ρréššion search and replace operation on the $data string. The regular E×ρréššion pattern (\d)(?=(\d\d\d)+(?!\d)) matches a digit (\d) followed by every three digits ((\d\d\d)+), as long as it is not followed by another digit ((?!\d)). The matched digits are then replaced with the digit ($1) followed by a dot (.).

Note that in PHP, the forward slash / delimiters used in JavaScript regular E×ρréššions are not required. Additionally, the g flag to perform a global search is not needed in PHP, as preg_replace by default replaces all occurrences of the pattern.
 

About this Thread

  • 1
    Replies
  • 433
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,088
Guests online
951
Total visitors
2,039

Forum statistics

Threads
2,283,730
Posts
29,017,530
Members
1,221,575
Latest member
nikkolai_nocturne
Back
Top