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?

4

2 回答 2

1

你可以使用str_replace()-

$link = '<a href="#" title="box">do something</a>';
$link = str_replace("</a>", '<i class="arrow"></i></a>', $link);
于 2013-06-08T03:16:39.433 回答
0

这是一个使用子字符串和连接的示例:

$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;
于 2013-06-08T03:22:01.007 回答