Posts Tagged ‘php’

Search and Replace PHP split() with Python

Tuesday, May 4th, 2010

A couple of days ago I upgraded to Ubuntu 10.04 which, in turn, upgraded my PHP version from 5.2 to 5.3. In PHP 5.3 they have completely deprecated the use of the split() function in favour of explode(). I did a quick search in my /work directory and it turns out that I have 832 files that are affected by this. All of them need to have split() replaced with explode() or the websites will throw errors every time they encounter it.

Here’s how I did it using an altered Python script I wrote a couple of years ago:

#!  /usr/bin/python
import os
import re

mydir = "/home/damian/work"

def doReplace(filePath):
	fin = open(filePath, "r")
	s = fin.read()
	fin.flush()
	fin.close()
	p = re.compile('(\s|\(|=)split\(')
	if p.search(s):
		fout = open(filePath, "w")
		s = p.sub(r'\1explode(', s)
		fout.write(s)
		fout.close()

for root, dirs, files in os.walk(mydir):
	for f in files:
		name, ext = os.path.splitext(f)
		if ext == '.php':
			doReplace(root + '/' + f)

(**UPDATE** I’ve switched the search and replace to use regular expressions because I found that ‘split()’ can be prefixed by a number of symbols but not others — i.e. don’t replace ‘preg_split()’)

Disable WSDL caching in PHP

Monday, September 22nd, 2008

If you are finding that your SOAP web service isn’t updating when you make changes you have to add the line ini_set("soap.wsdl_cache_enabled", "0"); to both the Server and the Client.

i.e.

<?php
class SoapController
{
 function HelloWorld($name)
 {
  return "Hello ". strrev($name) ;
 }
}
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer('http://example.com/soap.wsdl');
$server->setClass("SoapController");
$server->handle();
?>

and

<?php
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient('http://example.com/soap.wsdl', array('trace' => 1));
print_r($client->HelloWorld("Damian"));
?>

Pointy

Tuesday, September 16th, 2008

I do web development for a living. Almost two years ago I made the switch from being almost exclusively Microsoft and .NET to open source and along the way had to learn PHP (and a bit of Python for a while there too).

I’ve played around with many different web frameworks and been frustrated that they often force you to learn an alternative to SQL and/or some kind of restrictive templating language as well as the fact that most of them are too cumbersome for the types of sites that I’m generally required to produce.

So, for the last six months or so I’ve been refining my own MVC web development framework with the goal of keeping it lightweight, high performance and only using standard PHP and SQL where needed. And most of all, open source.

I present my baby, Pointy (the tiny but sharp development framework), which I’ve made available under a Creative Commons license. If you’re a PHP developer and you want to dabble with Pointy, please feel free to pay a visit, download it, play with it and if you find it useful go ahead and use it. I hope it’s as useful to you as it has been to me.

Some Handy Regular Expressions

Saturday, February 9th, 2008

At least something
/.+/

A positive integer
/^\d*$/

A decimal number
/^\d*\.?\d/

A valid email address
/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/

A valid web address
/^(http:\/\/)?\w+([\.-]?\w+)*\w+([\.-]?\w+)*(\.\w+)+(\/)?(\w+)?$/

A hexadecimal colour (6 chars)
/^#?[0-9A-Fa-f]{6}$/

How to use in Javascript:
var testString = "ff0000";
var pattern = /^#?[0-9A-Fa-f]{6}$/;
if (testString.match(pattern)) DoSomething();

How to use in PHP:
$testString = 'ff0000';
$pattern = '/^#?[0-9A-Fa-f]{6}$/';
if (preg_match($pattern, $testString) DoSomething();

How to use in C#:
string testString = "ff0000";
string pattern = @"^#?[0-9A-Fa-f]{6}$";
Match m = Regex.Match(testString, pattern);
if (m.Success) DoSomething();