0

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);
Unihedron
  • 10,902
  • 13
  • 62
  • 72
user1606423
  • 75
  • 1
  • 1
  • 7

2 Answers2

1

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).

UniversE
  • 2,419
  • 17
  • 24
0

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.

Sharky
  • 6,154
  • 3
  • 39
  • 72