Regular expression help needed

hello,

can you help me how to solve next thing using regular expressions…

i have string:

<span style="background-color: yellow;">String</span>

can you help me how to convert it into:

<a href="some-link.html?var=String">String</a>

tnx…

Salathe, thank you on your reply.

i have knowledge of regular expressions, but this goes beyond it.

more details about my problem…

i have php variable that contain some html string in it. that string can contain several parts of <span style=“background-color: yellow;”>String</span>, like

my text... <span style="background-color: yellow;">String 1</span> and more text. now more text <span style="background-color: yellow;">String 2</span>, and so on and on <span style="background-color: yellow;">String 3</span> and on.

now, all of <span style=“background-color: yellow;”>String</span> neex to be replaced with something like <a href=“some-link.html?var=String”>String</a>. it means that above text have to be:

my text... <a href="some-link.html?var=String 1">String 1</a> and more text. now more text <a href="some-link.html?var=String 2">String 2</a>, and so on and on <a href="some-link.html?var=String 3">String 3</a> and on.

also, url some-link.html is always the same, different is parameter (String 1, String 2, String 3)

you have to admit that it is not easy one
:slight_smile:

$replaced = preg_replace(
    '|\\Q<span style="background-color: yellow;">String</span>\\E|',
    '<a href="some-link.html?var=String">String</a>',
    '<span style="background-color: yellow;">String</span>'
);

copernicks, you’re going to have to give us more to go on that you have above. Which part(s) of the subject string are variable? Why would this need regular expressions? Would you like to know if this could be better|faster|more reliably|more efficiently achieved using something other than a regular expression?

Asking for A to be replaced by B, and expecting a solution for that individual case, might seem like a good way to get things done; but why not take the time to learn the tools, we’re happy to guide you. Give a man a fish… and all that.