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