Replacing the <a> tag with the <b> tag using PHP
3 answers
Although this is not optimal, you can do it with regular expressions:
$string = '<a title="title" href="http://example.com">Text</a>';
$string = preg_replace("/<a\s(.+?)>(.+?)<\/a>/is", "<b>$2</b>", $string);
echo($string);
This essentially says, look for the part of the line that has the form <a*>{TEXT}</a>
, copy {TEXT}
and replace this whole line with <b>{TEXT}</b>
.
+14
s/<a(?:\s[^>]*)?>([^<]+)<\/a>/<b>\1<\/b>/
The part between the first // searches for an opening tag (either <a> alone or with some parameters, in this case, free space \ s is required to avoid a match <abbrev> for example, for example), some text to be stored in brackets, and closing tag. The part between the second // is the replacement part, where \ 1 denotes the text matched by brackets in the first part.
See also the PHP function preg_replace . Then the final expression will look like this (verified):
preg_replace('/<a(?:\s[^>]*)?>([^<]+)<\/a>/i', '<b>\\1</b>', '<a href="blabla">Text</a>');
+1