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
Post a Comment