If you’re going to use replacement text that has ‘$’ or ” in it, chances are it is going to be treated as a backreference. Any parentheses you include in your regex will be captured and placed into the replacement text. If you don’t want this, you need to escape the ‘$’ as ‘$’ and the ” as ‘'. One example is if you wanted to replace ‘COST’ with ‘$12345’. The preg_replace_escape_back() function will sort this out:

Use this script to find if the user is on a Mac or a PC. Returns TRUE when on a Mac. $default is optional. If the script can’t determine either a Mac or a PC, it will return this.

This will expand a list of lists into a single list of all the items in Python. [[1, 2, 3, 4, [4, 5, 3, 2]], 2, 3, 4, [5, 2, 4, 5]] becomes: [1, 2, 3, 4, 4, 5, 3, 2, 2, 3, 4, 5, 2, 4, 5]

Well, I needed OrderedDict from Python 2.7+ but we had to use 2.5 for a software project, so I decided to do it the hard way and write it myself. It’s my first actual Python class, so it’s pretty cool that it works.

Here’s the language readme on how to create and edit language files which are used in Crayon to specify elements in languages using regular expressions and CSS rules for highlighting them.

I’ve been experimenting with cocos2d for iOS development and made a very simple program that grabs the touch locations on the screen and draws the points. It uses a C++ vector to store the CGPoint primitives and the draw: message takes care of drawing them, whenever we fill the vector with points.
Here’s the draw:
static std::vectorpoints;
...

-(void) draw {
...
	// Draw points
	glPointSize(4);
	for (int i = 0; i < points.size(); i++) {
		glColor4ub(0,255,0,255); // Green
		CGPoint p = points.at(i);
		ccDrawPoint(p);
		
		glColor4ub(255,255,255,255); // White
		CGPoint *vertices = &points[0]; // Vector to array
		ccDrawPoly(vertices, points.size(), NO);
	}
...
}
And here’s how the points are captured:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
	
	NSLog(@"TOUCH BEGIN %f %f", location.x, location.y);
	
	points.clear();
	points.push_back(location);
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
    
	points.push_back(location);
}

-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    points.clear();
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
	
    NSLog(@"TOUCH END %f %f", location.x, location.y);
	
	for (int i = 0; i < points.size(); i++) {
		CGPoint p = points.at(i);
		NSLog(@" TOUCH @ %f %f", p.x, p.y);
	}
}
Download Xcode Project

// Usage: trim_r( array(' 123 ', '   abc') );

// Trim a string or an array of strings recursively
function trim_r($array) {
    if (is_string($array)) {
        return trim($array);
    } else if (!is_array($array)) {
        return '';
    }
    $keys = array_keys($array);
    for ($i=0; $i
I was working on an Econometrics project and we needed to get a lot of data for a long list of tv shows. One good source was iMDB, but we didn’t feel like typing in the details of 1800 records… That’s when I had the idea of making a PHP scraper to get the info for us. I’ve polished it up and it’s easier to use now. The simplest example is:
// Perform a search and get info on 'inception'
IMDbScraper::get('inception');

// Output:

array(17) {
  ["name"]=>
  string(9) "Inception"
  ["desc"]=>
  string(205) "In a world where technology exists to enter the human mind through dream invasion, a highly skilled thief is given a final chance at redemption which involves executing his toughest job to date: Inception."
  ["date"]=>
  string(4) "2010"
  ["duration"]=>
  string(7) "148 min"
  ["director"]=>
  string(0) ""
  ["writer"]=>
  string(17) "Christopher Nolan"
  ["creator"]=>
  string(0) ""
  ["cast"]=>
  array(15) {
    [0]=>
    string(17) "Leonardo DiCaprio"
    [1]=>
    string(20) "Joseph Gordon-Levitt"
    [2]=>
    string(10) "Ellen Page"
    [3]=>
    string(9) "Tom Hardy"
    [4]=>
    string(12) "Ken Watanabe"
    [5]=>
    string(10) "Dileep Rao"
    [6]=>
    string(14) "Cillian Murphy"
    [7]=>
    string(12) "Tom Berenger"
    [8]=>
    string(16) "Marion Cotillard"
    [9]=>
    string(18) "Pete Postlethwaite"
    [10]=>
    string(13) "Michael Caine"
    [11]=>
    string(10) "Lukas Haas"
    [12]=>
    string(10) "Tai-Li Lee"
    [13]=>
    string(12) "Claire Geare"
    [14]=>
    string(12) "Magnus Nolan"
  }
  ["genres"]=>
  array(3) {
    [0]=>
    string(6) "Action"
    [1]=>
    string(9) "Adventure"
    [2]=>
    string(6) "Sci-Fi"
  }
  ["plot"]=>
  string(927) "Dom Cobb is a skilled thief, the absolute best in the dangerous art of extraction, stealing valuable secrets from deep within the subconscious during the dream state, when the mind is at its most vulnerable. Cobb's rare ability has made him a coveted player in this treacherous new world of corporate espionage, but it has also made him an international fugitive and cost him everything he has ever loved. Now Cobb is being offered a chance at redemption. One last job could give him his life back but only if he can accomplish the impossible-inception. Instead of the perfect heist, Cobb and his team of specialists have to pull off the reverse: their task is not to steal an idea but to plant one. If they succeed, it could be the perfect crime. But no amount of careful planning or expertise can prepare the team for the dangerous enemy that seems to predict their every move. An enemy that only Cobb could have seen coming."
  ["rating"]=>
  string(3) "8.9"
  ["max-rating"]=>
  string(2) "10"
  ["voter-count"]=>
  string(7) "413,580"
  ["user-review-count"]=>
  string(5) "2,095"
  ["critic-review-count"]=>
  string(3) "524"
  ["id"]=>
  string(9) "tt1375666"
  ["url"]=>
  string(36) "http://www.imdb.com/title/tt1375666/"
}
Some more examples:
// Import required files
require_once('imdb_scraper.php');
require_once('csv.php'); // CSV helper functions

/* SIMPLE SEARCH FUNCTIONS */

	/* Find full info using title, year is optional */
	var_dump( IMDbScraper::get('spongebob movie', '1999') );

	/* Find id and title of best search result, optional year */
	var_dump( IMDbScraper::find('spongebob movie', '1999') );
	var_dump( IMDbScraper::find('spongebob movie') );

	/* Return search results for title query, optional year */
	var_dump( IMDbScraper::search('spongebob') );
	var_dump( IMDbScraper::search('spongebob', '1999') );

/* SAVING TO / READING FROM CSV FILE */

	/* Write to CSV */
	var_dump( csv_write(IMDbScraper::search('spongebob'), 'example_files/example.csv') );
	/* Read from CSV */
	var_dump( csv_read('example_files/example.csv') );

/* ADVANCED SEARCH FUNCTIONS */

	/* Scrape information from a saved IMDb search page, given as an HTML string */
	var_dump( IMDbScraper::scrape_search( file_get_contents('example_files/saved_search.html')) );

	/* Scrape information from a saved IMDb title page, given as an HTML string */
	var_dump( IMDbScraper::scrape_info( file_get_contents('example_files/saved_title.html')) );