php - Matching 3 last numbers in a string with regex -


problem:

trying highlight last 3 numbers in string using regular expression.

code:

<?php     show_source('regex.php');      $string = "         780155overf i000000         tranfer domestic         000114         sthlm se ab     "; ?> <!doctype html> <html>     <head>         <title>regex match last 3 numbers</title>         <meta charset="utf-8">     </head>     <body>         <?php             echo nl2br(str_replace('/\d{3}(?=[^\d]+$)/g', '<span style="background-color:red;">$1</span>', $string));         ?>     </body> </html> 

desired outcome:

the numbers 114 should have red background color.

use:

print nl2br(         preg_replace('/\d{3}(?=[^\d]+$)/s',                       '<span style="background-color:red;">$0</span>',                       $string)            ); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -