Insert a string before the end of a link

2019-08-07 09:47发布

问题:

What's the fastest way to insert a string before the </a> tag?

<a href="..."> bla bla... </a>

回答1:

str_replace("</a>", "blabla</a>", $text);


回答2:

There are many, many ways to skin this cat, but here are a few common ones:

IN HTML:

<a href=""><?php echo $foo; ?></a>
<a href=""><?=$foo?></a>

IN PHP

echo "<a href=\"\">$foo</a>";
echo "<a href=\"\">{$foo}</a>";
echo "<a href=\"\">". $foo ."</a>";

Edit, you said "fastest", which I overlooked

Assuming we are talking about PHP, specifically: Supposedly using the comma for string concatenation in php is one of the fastest ways to build a string. But unless you are doing this an awful lot, this seems like an optimization that won't buy you very much.

echo "<a href=\"\">", $foo ,"</a>";


回答3:

<a href="..."> bla bla... <?php echo "string"; ?></a>