The title must be very confusing, but I have to admit, so am I when it comes to regex. My problem is the following:
I have an input string like:
{a href=www.google.com}Google{/a} {b}boldText{/b}
What I want to achieve is following:
<a href="www.google.com">Google</a> <b>boldText</b>
I have the following preg_replace function:
$input = preg_replace('/\{(\/)?'.$tag.'( [a-z]*\=)?([a-zA-Z0-9\.\:\/]*)?\}/', '<$1'.$tag.' $2"$3">', $input);
Where the tag is f.e. a
or b
or script
. The code is working so far, the only problem is that a tag without any attributes comes out like this:
<a href="www.google.com">Google</a> <b "">boldText</b>
Can I somehow add an if
condition around the $2"$3"
in the replacement argument of the preg_match
function, and if so, how can I do it?