Here is a quick example of how to use PHP’s base convert function in AS3. A lot of people use this function for shorter masked URLs like YouTube. Easy enough but took me a minute to find the equivalent in actionscript.
PHP:
function encode36($x){
return base_convert($x, 10, 36);
}
function decode36($x){
return base_convert($x, 36, 10);
}
Actionscript 3:
function encode36(x:Number):String{
return x.toString(36);
}
function decode36(x:String):int {
return parseInt(x,36);
}
In my previous post about email validation one user pointed out that this will not actually validate if a website’s domain actually exists. Here is a simple function that will do just that, determine if a website exists using PHP and cURL.
Edit: After searching a bit, I found another similar cURL solution that checks the actual headers. Since cURL can return HTTP code I don’t think all that extra code is necessary? I could be wrong!
function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
return true;
} else {
return false;
}
}
Assuming you have a table called `users` with birthday as `bday` for the column name, this will pull all users who are having a birthday in the next 7 days. This is very useful if you want to showcase your member’s birthdays on your website.
SELECT * FROM `users` WHERE DAYOFYEAR(curdate()) <= dayofyear(`bday`) AND DAYOFYEAR(curdate()) +7 >= dayofyear(`bday`) LIMIT 30;
There is of course one small problem with this, it’s not sorted. If we sort by `bday` ASC it’ll still be off since user’s are born on different years. What we really want is to sort by month and day only. I’m sure this can be done in MySQL but here is a PHP solution to take care of it.
$sql = "SELECT * FROM `users` WHERE DAYOFYEAR(curdate()) <= dayofyear(`bday`) AND DAYOFYEAR(curdate()) +7 >= dayofyear(`bday`) LIMIT 30;";
$query = mysql_query($sql);
$rows = mysql_num_rows($query);
if(!empty($rows)){
while($resultFind = mysql_fetch_array($query)){
$arr[] = array('bday' => $resultFind['bday'], 'bday2' => date("md", strtotime($resultFind['bday'])));
}
}
function compare($x, $y) {
if ( $x["bday2"] == $y["bday2"] )
return 0;
else if ( $x["bday2"] < $y["bday2"] )
return -1;
else
return 1;
}
usort($arr, "compare");
Using usort() we can sort based on the month and day. You now have your list sorted
About 7 months ago MTV released a promising API MTVN which opened up a ton of artist metadata and the ability to embed videos from MTV, VH1, CMT and Logo. Looks like they’re pulling the plug come March. As a developer and someone who uses the API, I am pretty upset about this. Thanks MTV :/
Have you seen this error? If you work with Flash actionscript and Javascript, you probably have. It can be pretty frustrating the first time you see this error since it’s not very informative. In my experience, this happened when I was trying to communicate with actionscript via Javascript using ExternalInterface call. The problem was sure enough a security issue with Flash. The solution for “Error calling method on NPObject!” is to add:
System.security.allowDomain("mydomain.com");
Add this to the Flash file you’re communicating with. Keep in mind that www.mydomain.com is different from mydomain.com to Flash.
I see a lot of sites still using the long default Amazon S3 url so I thought this might be helpful to someone. Lets say I setup a bucket for images on this domain at images.wrichards.com.s3.amazonaws.com to serve all my images. I don’t really want to type out the extra s3.amazonaws.com bit everytime I link to an image.
There is a quick and easy solution to change your domain with CNAME:
images.wrichards.com CNAME images.wrichards.com.s3.amazonaws.com
Just add a CNAME record with that information and you should be good to go (obviously change my domain to yours). You can add your CNAME in your DNS entry More Info here. I can now access my bucket via the custom domain images.wrichards.com.The bucket name must match and be lowercase for this to work properly.

JQuery. Write less, do more
jQuery.fn.sort = function() {
return this.pushStack( [].sort.apply( this, arguments ), []);
};
function sortAlpha(a,b){
return a.innerHTML > b.innerHTML ? 1 : -1;
};
$('ol li').sort(sortAlpha).appendTo('ol');
Short and sweet. The result is a in place sort based off the innerHTML alphabetically.
In prior version of PHP you had to use some regex solution to validate an email address, now in PHP5 it’s as simple as:
function isValidEmail($email){
return filter_var(filter_var($email, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
}
if(isValidEmail('email@domain.com')){
//valid
} else {
//not valid
}
I’ve been using PHP5 ever since it was released but didn’t look into the PECL Filter extension until recently. Pretty handy.
Please note that I also added the FILTER_SANITIZE_EMAIL filter which removes all illegal e-mail characters from a string. This is not needed if you only want to validate and not clean the input.
This has got to be one of the coolest things I have come across in a long time. Sphinx a standalone MySQL full-text search engine. I’ve been working with it for a couple weeks now and I must say, this thing is blazin’ fast. I was most impressed by performance on large data sets (60+ million rows). Even when preforming sorts, it’s really fast. Oh, they have a PHP API too! If you’re looking to super charge your mysql search, give it a shot!
More on this with examples later.
A few weeks back I ran into a rather odd situation when trying to load an xml file via Actionscript across multiple domains. No matter what I did, it would not load! Typical way to resolve this is to put a crossdomain.xml granting access to the swf host location right? Well, if you’re planning on using Lighty, a good thing to keep in mind when installing is to set the proper mimetype.assign options in the configuration file. If you do not set this it will default to “application/octet-stream” which for some reason Flash (actionscript) does NOT like and will ignore crossdomain.xml. I know it’s kind of a newbie mistake but I’m sure one of you will come across the same issue when using it for the first time like I did.