Chris Tate-Davies

An archive of helpful tit bits of information for development, and probably some stuff that is incomplete, wrong or boring…

Online regex -> php tool

Posted on | June 22, 2010 | No Comments

I’ve found an excellent online tool, where you can give it a regex, and it will spit out the PHP syntax for use with preg_replace, preg_match, etc.

I’m forever getting ‘Unknown modifier’ errors when trying to insert regex’s into my PHP code. This sorts it out for you.

http://regex.larsolavtorvik.com/

I wrote one of the first social networking sites

Posted on | May 13, 2010 | No Comments

Its not a claim to fame, or a preempt to a court case to a ‘big hitter’, but back in the year 2000, when I was a fledgling junior programmer, I wrote an ASP web site based around a bunch of ‘buddies’. You could add people to your list, message them, upload a picture (just the one) of yourself, and view other’s profiles.

This sounds very familiar, right?

Read more

Getting Ant to run on Windows 7 (64bit)

Posted on | April 23, 2010 | No Comments

If like me, you use Ant to deploy builds, etc, and you have migrated to Windows 7 – you may well have troubles getting ant to run.

It all worked flawlessly under Ubuntu, but Windows is a different beast – here’s a step by step guide to getting it working.

Read more

Remove svn folders from Ubuntu

Posted on | April 9, 2010 | No Comments

When backing up a source code working copy, you don’t always want all the little .svn folders everywhere.

You can “export” the working copy, or if you forgot, you have remove them all.

Change the the folder containing the working copy (this is very important*)

cd /home/username/www/sitename/

And then remove them all, recursively with this command:

find -name ".svn" -exec rm -rf {} ;

This will find all .svn files/folders in the current folder, and remove them and everything in them.

*if you do not ensure you are in the right folder, this command will remove all recursive folders, so if you are in the web root, it will remove all the svn information from all your sites… This is very important (that’s twice I’ve said that, so it must be)

Cisco VPN client on Windows 7 64bit

Posted on | April 8, 2010 | 1 Comment

If you’re trying to get Cisco VPN client installed on Windows 7 – 64bit then you may as well give up. I’ve tried just about every method discussed on the internet.

However, http://www.shrew.net/download have a VPN client that will allow you to import your PCF file and it works!

Export Thunderbird contacts to Outlook

Posted on | April 8, 2010 | No Comments

I wanted to export my Thunderbird Contacts to Microsoft Outlook 2010 beta.

Jesus, if that wasn’t the most stressful export ever! Thunderbird can only export as CSV or tab separated – or LDAP.

Outlook says it can import from CSV and Tab separated, however it just crashes with mapping errors.

I tried dragging them from TB and then dragging into Contacts on Outlook, but that just resulted in a new empty contact, with an attachment of the email address in a text file. Helpful.

Anyway, I found this Thunderbird Addon that will export to vCard (The standard Address Card I feel) and then I could drag these into Outlook.

http://nic-nac-project.de/~kaosmos/morecols-en.html

However, Outlook feels the need to open each vCard as a new Contact screen, so you then have the joys of closing as many screens as you have contacts just imported. So it could be hundred and hundreds!

I do wonder if the designers of these software packages ever have to use this functionality. Perhaps they should.

Programmers Excuses

Posted on | April 8, 2010 | 1 Comment

Programmers Excuses

Programmers Excuses

Installing Tahoma font in Ubuntu

Posted on | April 6, 2010 | No Comments

The stylesheet of our intranet is primarily Tahoma, and Firefox on Ubuntu doesn’t look good.

To install the Microsoft Core Fonts package in Ubuntu, run the following in your terminal:

sudo apt-get install msttcorefonts

And then this should go away and get them. Lovely.

NB, if this doesn’t take immediate effect, you can rebuild the font cache by running

sudo fc-cache -fv

Now, the Tahoma font is excluded these days from this package, so you’ll need to install that one manually, by following the instructions found here

Regex – “The” searching

Posted on | March 26, 2010 | 10 Comments

Say you have a list of movie titles, and you want to either sort them, or search through them, and some of them have “The ” at the start, for example:

  • The Simpsons
  • Simpson Street

When doing a MySQL search:

SELECT * FROM movies WHERE title LIKE "The Simp%";

Would only return the first row. But if you are working in a company where there is no standard set, the movie title could be formatted as “Simpsons, The” – and then, it won’t be found.

To solve this, you could replace the “The ” letters with blank, and then sort out the field contents during the query:

$str_query = preg_replace("/(title like "(the )(.*)%")/i",
    "REPLACE(LOWER(title), "the ", "") LIKE ("$3%")",
    $str_query);

This will change :

"(title LIKE "The Simpsons")"

to,

"(title LIKE "Simpsons")"

But, the (the) in line 2 tells PHP to only replace it if starts with “The ” (case insenstive).

However, what if you want to search for “the”  (not sure why you would…)

You need to do a negative lookahead, to tell the expression to only carry on, if the search phrase is not exactly “the”

if (preg_match("/(title like "(?!the)(.*)%")/i", $str_query)) {

The (?!the) is the readahead.

(.*) matches any string but it is greedy and you have to be carfeul that it doesn’t just accept everything to the end of $str_query. (but its okay in our case, as we are looking for % (the LIKE wildcard))

After all this, we can run:

SELECT * FROM movies WHERE $str_query;

But what about sorting? All the titles beginning with “The” will appear in the T section. Whereas really, we want the Simpsons to appear in the S section.

Add an easy ORDER BY clause here:

SELECT * FROM movies WHERE $str_query ORDER BY (REPLACE(title, "the ", "") ASC;

Sorted!

MySQL – Order by certain value first

Posted on | March 23, 2010 | 1 Comment

Ever wanted to sort a resultset of data, by value, but I wanted a couple of exceptions to appear at the top?

SELECT country, population,
CASE country
WHEN 'United Kingdom' THEN 0
WHEN 'United States' THEN 1
WHEN 'New Zealand" THEN 2
ELSE country
END AS countrySort
FROM countryList 
ORDER BY countrySort ASC;

This will sort the results by country, but with UK, US, NZ as the top three.

« go backkeep looking »
  • Chris Tate-Davies

    Hello there. This is my little "repository" on the world wide web. Its for nothing more than documenting things that I might need again in the future. You could describe it as an extension to my memory.

    Also an online collaboration of my thoughts through the day. I'll try to keep the real random stuff out and keep the blog on course.

    Thanks for stopping by... Hope you find what you're looking for...

  • Tags