2

I have this link like so:

$link = '<a href="#" title="box">do something</a>';

Now I need to add html to the end of the text within the anchor tag so it ends up like this:

$link = '<a href="#" title="box">do something<i class="arrow"></i></a>';

I have researched around and my conclusion is that I probably have to use regular expression but I don't know where to start. I know this can be done easily via JS but I need it done in PHP.

Any ideas?

2 Answers2

1

You could use str_replace()-

$link = '<a href="#" title="box">do something</a>';
$link = str_replace("</a>", '<i class="arrow"></i></a>', $link);
Sean
  • 12,443
  • 3
  • 29
  • 47
0

Here is an example using substrings and concatenation:

$link = '<a href="#" title="box">do something</a>';
$addition = '<i class="arrow"></i>';

$formatted_str = substr($link, 0, strlen($link) - 4) . $addition . substr($link, strlen($link) - 4);

echo $formatted_str;
BIOS
  • 1,655
  • 5
  • 20
  • 36