KBD

Keith Devens .com

Thursday, August 28, 2008 Flag waving
Your best? Losers always whine about their best. Winners go home and fuck the prom queen. – John Mason (The Rock)
← HTTrack Website CopierNew Perl class one-liner →

Daily link icon Wednesday, August 6, 2003

Convoluted Regular Expression of the Day

This is the regular expression and its replacement that parses URLs in my markup parser. My markup parser is what formats all the text on my weblog, including all comments. These are fed to preg_replace to do their job.

<?php

$regex 
"/(?<=^|[\s({])(\w+:\/\/[\w.,\/?+~&=_:;#$%{}-]+[^\s.?!:,()<>[\]'\"&]+?)/e"#urls

$style '\'<a href="\'.\'$1\'.\'">\'.((($len = strlen(\'$1\')) > '.
$flags['max_url_length'].') ? (substr(\'$1\',0,'.
ceil($flags['max_url_length']/2).').\'...\'.substr(\'$1\',$len-'.
(
floor($flags['max_url_length']/2)-3).')) : \'$1\').\'</a>\'';
?>

The code's been changed a little bit (for instance, broken up so it's not too long on one line), but you get the idea. Smiley As you can see, this code wasn't built from any formal grammar of URLs... it's just been built ad hoc as I've come across URLs it it didn't parse. It's possible that it's horribly inefficient compared to what it could be. If you see anything that should be different, let me know Smiley

Besides recently fixing the code to allow tildes in URLs (which should have been in there a long time ago), I finally took a few minutes today to fix the code so that really long links won't blow out my layout. Works exactly right.

By the way, if you want to implement this code, make sure you stick two calls to htmlspecialchars() in there, otherwise it won't work correctly for ampersands, etc.

Note: updated the regex a bit.

← HTTrack Website CopierNew Perl class one-liner →

Comments XML gif


Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 1 posts

Recent comments XML

new⇒Girls, please don't get breast implants

http://war-11.ucufyod.net​http://jewelry-26.wohiles.net​http://games-52.ruz...

Randolph: Aug 28, 5:16am

Generated in about 0.156s.

(Used 8 db queries)

mobile phone