I've got a string that contains some crappy Microsoft characters. I want to remove all the <a name> tags and just replace them with <i> italics tags around the text inside. However the string contains multiple a name tags as well as <a href tags which I dont' want to be touched.

This is the string:

$b2 = '<A name=OLE_LINK2>test<b>test</b></A><A name=OLE_LINK1>Any member who wishes to obtain a copy of <b><i>Roark v. Commissioner</i></b> may do so through the following means: (1) use hyperlink above next to "Major References,"</A> asfdasfds';

I'm using this preg_replace:
$body = preg_replace('/<a name=OLE_LINK([0-9])>(.*)<\/a>/i', '<i>\2</i>', $b2);

But its only replacing the first a name tag and its not replacing the </a>'s at all.

any help would be greatly appreciated.

    You are using the greedy match of the regular expression, which means that everything between the first and the last 'a' tag is a match.

    In order to fix that, you have to use the non-greedy match, i.e.

    $body = preg_replace('/<a name=OLE_LINK([0-9])>(.*?)<\/a>/i', '<i>\\2</i>', $b2);

    Note the question mark in the second parentheses.

      worked like a charm. thanks much.

        Write a Reply...