Reputation: 2636
I have some strings they are combinations of numbers and letters both like:
A555D1E
, 452AE
, now i want to set a different color for numbers and letters.
So I need to surround numbers in string with html tags like span
or b
and result should be like this:
html:
<div class="spec-text">A<span class="n">555</span>D<span class="n">1</span>E</div>
css:
spec-text{
color:red;
}
spec-text .n{
color:green;
}
if there is some function like:
function surroundNumbersByTag($string, $tag, $classes){
// codes that return this string:
// 'A<span class="n">555</span>D<span class="n">1</span>E'
}
is there anyway ?
Upvotes: 0
Views: 65
Reputation: 5062
Try
function surroundNumbersByTag ($string, $tag, $classes)
{
return preg_replace("~([0-9]+)~", "<$tag class='$classes'>$1</$tag>", $string);
}
echo surroundNumbersByTag('A555D1E', 'span', 'n');
Upvotes: 1