Extract Email Addresses

Just pass the string (e.g. the body part of an email) to the function, and it returns an array of Email Addresses contained in the String.

function extract_emails_from($string) {
         preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
         return $matches[0];
}

If you catch the return …

Avatar of Chris Coyier
Chris Coyier on

View Source of RSS Feed in Firefox

Firefox likes to assume when you click on a link to an RSS feed that you want to subscribe to it in some fashion. That might be true most of the time, but sometimes you just want to see the …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Global Variables

Declare variable outside of the function…

var oneVariable;

function setVariable(){
    oneVariable = "Variable set from within a function!";
}

function getVariable(){
    alert(oneVariable); // Outputs "Variable set from within a function!"
}

Or… attach it to the window object

function setValue() 
Avatar of Chris Coyier
Chris Coyier on

What Beautiful HTML Code Looks Like

I originally wrote this over two years ago. It was getting a little long in the tooth, especially now that HTML5 has come along and made HTML far more beautiful than even XHTML 1.1 was. So I updated it!

I …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Perform Function on Each Item of an Array

array_map() takes two arguments. The first argument is the function to perform on the second argument (which is an array). Returns a new array.

$original = array('<p>Paragraph</p>', '<strong>Bold</strong>');
$new = array_map('strip_tags', $original);

// $new is now array('Paragraph', 'Bold');
Avatar of Chris Coyier
Chris Coyier on

Create URL Slug from Post Title

Regular expression function that replaces spaces between words with hyphens.

<?php
function create_slug($string){
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>
Avatar of Chris Coyier
Chris Coyier on

Parse JSON

<?php
   $json ='{"id":1,"name":"foo","interest":["wordpress","php"]} ';

   $obj=json_decode($json);

   echo $obj->interest[1]; //prints php
?>
Avatar of Chris Coyier
Chris Coyier on