Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2012 16:45
Show Gist options
  • Save anonymous/2931420 to your computer and use it in GitHub Desktop.
Save anonymous/2931420 to your computer and use it in GitHub Desktop.
How to escape user input for display on a page, and turn URLs into links.
<?php
$content = '
Text with newlines, possible XSS attacks and URLs.
<script type="text/javascript">alert("this could be an XSS attack.");</script>
The URL to my github page is https://github.com/geon.
';
/*
0 - The URL as written in the text.
|-------------------------------------------------|
2 - The "s" in "https".
|--|
3 - Aything after "http://".
|-------------|
4 - URL without heading "http://", but with "www.".
|------------------|
(http(s)?:\/\/([^\s]*[^\s\.]))|(www\.[^\s]*[^\s\.])
The "body" of the URL is matched with "[^\s]*[^\s\.]", meaning anything until
whitespace, but not including trailing dots. (A URL is commonly written in the
end of a scentence.)
Reconstruct the URL with "http${2}://${3}${4}".
The backreference to 2 means we can write out the "http" to make the
URLs missing it work, and still make https work.
Both 3 and 4 are used, since only either one will will ever match.
*/
// Wrap the content in a p-tag.
$content_htmlized = '<p>'.
// Replace double newlines with a new p-tag and single newlines with a br-tag.
strtr(
// Add link-tags to URLs.
preg_replace(
'/(http(s)?:\/\/([^\s]*[^\s\.]))|(www\.[^\s]*[^\s\.])/uis',
'<a target="_blank" href="http${2}://${3}${4}">${0}</a>',
htmlspecialchars($content)
),
array("\n\n" => "</p>\n<p>", "\n" => "<br />\n")
).
'</p>';
// The htmlized content can be printed straight out on the page.
print($content_htmlized);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment