Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to replace a specific tag and its endtag?

Tags:

regex

php

I'm making a little chat, and I got a problem: I disabled HTML, but there must be a possibility to insert links. So I decided to mark them as

[LINK]http://anylink.com[/LINK].

Now, there could be multiple links like this in the text. How can I replace all of them with the following:

<a href="http://anyling.com">http://anylink.com</a>

Thank you for your help, I dont get it with the href tag :S

like image 946
Florian Müller Avatar asked Dec 22 '25 07:12

Florian Müller


1 Answers

$string = preg_replace('%\[LINK\](.*?)\[/LINK\]%','<a href="$1">$1</a>', $string);
like image 117
Wrikken Avatar answered Dec 23 '25 20:12

Wrikken