note 101592 added to function.htmlspecialchars
| From: | Thomasvdbulk at gmail dot com | Date: | Tue, 28 Dec 2010 18:05:34 +0000 |
| Subject: | note 101592 added to function.htmlspecialchars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175283@lists.php.net to get a copy of this message | ||
i searched for a while for a script, that could see the difference between an html tag and just <
and > placed in the text,
the reason is that i recieve text from a database,
wich is inserted by an html form, and contains text and html tags,
the text can contain < and >, so does the tags,
with htmlspecialchars you can validate your text to XHTML,
but you'll also change the tags, like <b> to <b>,
so i needed a script that could see the difference between those two...
but i couldn't find one so i made my own one,
i havent fully tested it, but the parts i tested worked perfect!
just for people that were searching for something like this,
it may looks big, could be done easier, but it works for me, so im happy.
<?php
function fixtags($text){
$text = htmlspecialchars($text);
$text = preg_replace("/=/", "=\"\"", $text);
$text = preg_replace("/"/", ""\"", $text);
$tags = "/<(\/|)(\w*)(\
|)(\w*)([\\\=]*)(?|(\")\""\"|)(?|(.*)?"(\")|)([\
]?)(\/|)>/i";
$replacement = "<$1$2$3$4$5$6$7$8$9$10>";
$text = preg_replace($tags, $replacement, $text);
$text = preg_replace("/=\"\"/", "=", $text);
return $text;
}
?>
an example:
<?php
$string = "
this is smaller < than this<br />
this is greater > than this<br />
this is the same = as this<br />
<a href=\"http://www.example.com/example.php?test=test\">This
is a link</a><br />
<b>Bold</b> <i>italic</i> etc...";
echo fixtags($string);
?>
will echo:
this is smaller < than this<br />
this is greater > than this<br />
this is the same = as this<br />
<a href="http://www.example.com/example.php?test=test">This
is a link</a><br />
<b>Bold</b> <i>italic</i> etc...
I hope its helpfull!!
----
Server IP: 83.137.20.135
Probable Submitter: 83.85.88.164
----
Manual Page -- http://www.php.net/manual/en/function.htmlspecialchars.php
Edit -- https://master.php.net/note/edit/101592
Del: integrated -- https://master.php.net/note/delete/101592/integrated
Del: useless -- https://master.php.net/note/delete/101592/useless
Del: bad code -- https://master.php.net/note/delete/101592/bad+code
Del: spam -- https://master.php.net/note/delete/101592/spam
Del: non-english -- https://master.php.net/note/delete/101592/non-english
Del: in docs -- https://master.php.net/note/delete/101592/in+docs
Del: other reasons-- https://master.php.net/note/delete/101592
Reject -- https://master.php.net/note/reject/101592
Search -- https://master.php.net/manage/user-notes.php