Marcos Placona Blog

Programming, technology and the taming of the web.

Category: Regular Expressions

Regular Expressions provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters

How to return tag contents with regular expressions

Reading time: < 1 minute

How to return tag contents with regular expressions
(Photo: Jeff Kubina)

As most of you already know, I LOVE regular expressions, and think they are great to solve simple and complex tasks involving strings.
One thing I usually need to do, is extract contents from inside HTML or XML tags.
Take the following XML document for example: Continue reading

Apache 101 – Case insensitive URL’s

Reading time: 1 – 2 minutes

This is only a quick Apache tip for when you are using mod_rewrite.

I’ve been working on some rewriting lately, and noticed that when you use them, the pattern applied must match exactly, otherwise you will either get error, or your pattern will never find a match.

Obviously you have a few options when writing your rewrite rules to make it case insensitive, but that means you will have to use it on every single rule.

In my case, I simply want everything to come though as lower case, so even if you hit any of my pages with an upper case URL, it will automatically be rewritten to the same thing, but in lower case, and return 301, so the search engines know the page was permanently moved.

This is how I do it:

#Make URL's lower case
RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

The RewriteMap directive needs to be placed on your httpd.conf or VHOST, as it won’t work on directory level or .htaccess. Everything else can go on your directory or .htaccess.

Tip of the day: Regular Expression Generator

Reading time: 1 – 2 minutes

I have just came across this very nice service which promises to write Regular Expressions for you.
We all know how painful writing a regular expression can be, and on this website, you can not only have the pattern created for you, but also have it translated into the most common languages on the web.
I’ve tried it myself, and can say it’s pretty darn good, although it’s automatic generator gives you a lot of “functional-crap” sometimes, it’s good for when you’re in a hurry and need to have a pattern up and running, or is not very confident with regexps.
Check it out here