KBD

Keith Devens .com

Sunday, October 12, 2008 Flag waving
REMEMBERS- HEEL BARES DURID! BARE DURIDS IS STORNG FREND! – Alamo

Archive: August 06, 2003

← August 05, 2003August 07, 2003 →

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 Copier

I've mentioned HTTrack before, but that's kind of the point here. One of the main reasons I keep my weblog is so that I can keep track of links I've come across so I can find them when I need them. I finally had a need for HTTrack, which I didn't at the time, so I found it, used it, and it worked flawlessly. This is how my blog is supposed to work Smiley I recommend the program, by the way.

← August 05, 2003August 07, 2003 →
October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



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

Recent comments XML

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.103s.

(Used 7 db queries)

mobile phone