Perl str_replace

August 9th, 2009

$s =~ s!\Q$search!$replacement!g;

\Q will quote a string until \E (if \E is ommitted will quote the whole string). You need to quote the string because s/// uses regexp;

You can view more Perl written in PHP here.

Bookmark and Share

Netbeans + CakePHP – syntax highlighting for .ctp files

August 6th, 2009

Tools -> Options -> Miscellaneous -> Files

New File Extension (ctp)

Associate with PHP

Viola! CTP files are now treated as PHP files!

Bookmark and Share

Mysql from an external file

July 26th, 2009

From the command-line:

mysql -u username -ppassword database < filename

From within MySQL:

Log into mysql i.e. mysql -u -p [database]
mysql> source [filename]
Bookmark and Share

PHP – Split with delimiter caputre

July 23rd, 2009

Split a string on a regular expression and also capture the delimiter.  This can be useful if you have regular expressions like /(cat|dog|fish)/.

$arr = preg_split($rx, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0; $i < count($arr); $i+=2) {
  $chunk = $arr[0];
  $delim = $arr[1];
}
Bookmark and Share

Python – Getting dot to match newlines as well

July 23rd, 2009

re.DOTALL / re.S – means dot will match newlines

re.MULTILINE / re.M – means that ^$ will only match start and end of string.  By default they match start and end of each line

match = re.search(r'^(.+lorem ipsum)(.+)(donor kebab.+)$', s, re.DOTALL + re.MULTILINE)




Bookmark and Share

Perl – quick read text files scripts

July 21st, 2009

These two Perl utility scripts are used to quickly read a text file into either a string or an array.

sub read_file_into_string {
  my $s = '';
  open FH, @_[0];
  for (<FH>) { $s .= $_; }
  close FH;
  return $s;
}
sub read_file_into_array {
  my @lines = ();
  open FH, @_[0];
  for (<FH>) {
    chomp;
    push @lines, $_;
  }
  close FH;
  return @lines;
}
Bookmark and Share

VBA – write to UTF file (with BOM)

July 14th, 2009

Dim objStream
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Charset = "UTF-8"
objStream.Position = 0
objStream.WriteText sTxt
objStream.SaveToFile sFileName, 2
objStream.Close

Bookmark and Share

Python MySQL utf connection + dictionary recordsets

July 14th, 2009

# -*- coding: utf-8 -*-
import MySQLdb, MySQLdb.cursors
db_connection = MySQLdb.connect(user="root", passwd="pass",
db="mydb", cursorclass = MySQLdb.cursors.DictCursor,
use_unicode = True)
db = db_connection.cursor()
# assuming that id=1271 has a val that has utf "µ" in it
sql = """
SELECT *
FROM vals
WHERE id = 1271
""" % ()
db.execute(sql)
r = db.fetchall()[0]
val = r['val']
print val
print type(val)

Bookmark and Share

Python unicode + xml.dom.minidom + write to file

July 14th, 2009

# -*- coding: utf-8 -*-
import xml.dom.minidom, codecs
impl = xml.dom.minidom.getDOMImplementation()
dom = impl.createDocument(None, "root", None)
root_el = dom.documentElement
ascii_el = dom.createElement('ascii')
root_el.appendChild(ascii_el)
ascii_el.appendChild(dom.createTextNode('abc'))
utf_el = dom.createElement('utf')
root_el.appendChild(utf_el)
utf_el.appendChild(dom.createTextNode(unicode('µ 6000', 'utf-8')))
# There's a couple of ways of dealing with output:
if True:
# Will write a utf encoded file without BOM
# <?xml version="1.0"?>
# Will print "µ" to the console
# type(xmlstr) == 'unicode' i.e. unicode string
xmlstr = dom.toxml()
f = codecs.open('utf_test.xml', 'w', 'utf-8')
f.write(xmlstr)
f.close()
print xmlstr
print type(xmlstr)
else:
# Will write a utf encoded file without BOM (same as above)
# <?xml version="1.0" encoding="utf-8"?>
# Will print "??" to the console
# type(xmlstr) == 'str' -- i.e. byte string
xmlstr = dom.toxml('utf-8')
f = open('utf_test.xml', 'w')
f.write(xmlstr)
f.close()
print xmlstr
print type(xmlstr)

Bookmark and Share

Python – pretty dictionary access for objects

June 28th, 2009

Instead of hash like access i.e. val = hash['key']

You can achieve a prettier val = obj.key :

class Obj:
..def add(self, k, v):
....self.__dict__[k] = v

o = Obj()
o.add(key, val)

Bookmark and Share