I would like to change a string from:
<a href....>*</a>
to:
<article><a href=....>*</a></article>
I have tried this but my understanding of RegEx is too bad.
$n = '/<a (.*)[^>]>/';
$h = '/<article><a(.*)[^>]>/i','/<\/a></articla>/';
$reg = preg_replace($n, $h, $content);
Your solution would match <a href...> but not the closing element.
Try this:
$n = '/(<a [^>]*>([^<]*<(\/[^a])|[^\/])*\/a>)/i';
$h = '<article>${1}</article>';
$reg = preg_replace($n, $h, $content);
Edit:
now respects child elements
Explenation:
<a [^>]*>
Matches the start tag.
(
[^<]*<
Finds the next tag.
(\/[^a])|[^\/]
)*
Ensures, that the next tag is not a closing </a> and so matches every other tag.
\/a>
Matches the closing </a>, finally. (Note: the < has already been matched).
if $content
is just a "string" and not html, then just go for the simple way:
$content = str_replace('</a>','</a></article>',str_replace('<a href=','<article><a href=',$content));
Simple, clean, no need for regex.
If $content
is NOT just a "string" but it is html, then nor str_replace, nor regex will help. You will need a html parser.