I have a PHP script which processes user input. I need to escape all special characters, but also make links clickable (turn them into <a>
elements). What I need is:
function specialCharsAndLinks($text) {
// magic goes here
}
$inp = "http://web.page/index.php?a1=hi&a2=hello\n<script src=\"http://bad-website.com/exploit.js\"></script>";
$out = specialCharsAndLinks($inp);
echo $out;
The output should be (in HTML):
<a href="http://web.page/index.php?a1=hi&a2=hello">http://web.page/index.php?a1=hi&a2=hello</a>
<script src="http://bad-website.com/exploit.js"></script>
Note that the amperstand in the link stays in the href
attribute, but is converted to &
in the actual content of the link.
When viewed in a browser:
http://web.page/index.php?a1=hi&a2=hello <script src="http://bad-website.com/exploit.js"></script>