Reputation:
I want to do something like stackoverflow. actually changing this style []()
to this style <a href=""></a>
. here is my try:
$str = '[link](#)';
$str = str_replace('[','<a href="',$str); // output: <a href="link](#)
$str = str_replace(']','">',$str); // output: <a href="link">(#)
$str = str_replace('(','',$str); // output: <a href="link">#)
$str = str_replace(')','</a>',$str); // output: <a href="link">#</a>
but now, I need to change link
with #
, how can I do that ?
Upvotes: 2
Views: 109
Reputation: 59691
You want to take a look at preg_replace()
, with this you can use a regex to replace it, e.g.
$str = preg_replace("/\[(.*?)\]\((.*?)\)/", "<a href='$2'>$1</a>", $str);
regex explanation:
\[(.*?)\]\((.*?)\)
Upvotes: 3