Preg Replace Assistance

I’m having trouble getting the pattern right for a preg_replace.

I’m wanting to link all usernames that are located inside bold HTML tags and a colon in a string.

So I’m wanting to replace all instances of:



<B><a href=“/username”>username</a></B>:

There are many usernames listed. All inside <B></B>:

Thanks for your help.

I was able to get it working with this regular expression:

preg_replace(‘/<b>([A-Za-z0-9-]+)<\/b>:/i’,'<b><a href=“/$1”>$1</a></b>: ',$var);

this is the basic example, just adjust according to your needs

$var = '<B>username</B>:';

//username should be 1 or more characters
preg_replace('/<b>(.+)<\\/b>:/i','<b><a href="/$1">$1</a>',$var);

//sample when username should be 8 characters only
preg_replace('/<b>(.{8})<\\/b>:/i','<b><a href="/$1">$1</a></b>',$var);

//sample when username should be more than 7 characters
preg_replace('/<b>(.{8,})<\\/b>:/i','<b><a href="/$1">$1</a></b>',$var);

the (dot) there means it will match any single character except line break characters

the (i) delimiter there makes it case insensitive so for example it matches both B and b