0

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 ?

1 Answer 1

1

Try

function surroundNumbersByTag ($string, $tag, $classes)
{
    return preg_replace("~([0-9]+)~", "<$tag class='$classes'>$1</$tag>", $string);
}

echo surroundNumbersByTag('A555D1E', 'span', 'n');
Sign up to request clarification or add additional context in comments.

2 Comments

$classes is not array
It's GREAT @gareth-parker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.