Ronnie Ronnie - 7 months ago 9
PHP Question

PHP - How to find numbers and bold that numbers in a string

How I can make all the numbers in a string bold?

For example:

$st_nu = "Today123"
would become
Today<b>123</b>
.

Answer

You can use a regular expression to bolden only numbers.

preg_replace('/(\d+)/', '<b>$1</b>', $st_nu);

What this does is matches any number 1 or more characters long, then surrounds it with the <b> tag (bold).

Using your example, you'd get back the following:

Today123

Comments