Gomi
Gomi

Reputation: 662

PHP preg_replace exclude < and >

I need to change this code:

$output = preg_replace("#&lt;a ([^&gt;]*)&gt;([^&lt;]*)&lt;\/a&gt;#", "<a href=\"$1\">$2</a>", $output)

to make from &lt;a some.url&gt;description&lt;/a&gt; to HTML code <a href="some.url">description</a>, but after i changed < and > to &lt; and &gt; it doesn't make what I wanted.

Thank you for your help.

EDIT:

I wouldn't use html_entity_decode(), because I want this for enabling slightly simplified HTML tags, but the most of HTML i would have disabled.

Input for my function is a variable, obtained from a form:

Firstly the code replaces all < and > with &lt; and &gt;.

Than it changes back some text-modifying static HTML tags, for example <b> and </b>.

But in the end, i want to change every link inputed as <a some.url>link</a> (and than by code changed to &lt;a some.url&gt;link&lt;/a&gt;) to be changed by preg_replace() into standard HTML code <a href="some.url">link</a>

Upvotes: 1

Views: 2205

Answers (1)

S.Visser
S.Visser

Reputation: 4725

http://php.net/manual/en/function.html-entity-decode.php

$str = "this 'quote' is &lt;b&gt;bold&lt;/b&gt;"
html_entity_decode($output)
// output This 'quote' is <b>bold</b>

http://php.net/manual/en/function.htmlentities.php

$str = "This 'quote' is <b>bold</b>";
htmlentities($output);
// output this 'quote' is &lt;b&gt;bold&lt;/b&gt;

Upvotes: 2

Related Questions