PHP: Validate an IP Address

So you need to check if some string is a valid IP address. You could simply test it against a regular expression:

[cc lang=’php’ ]
function is_valid_ipv4($ip)
{
return preg_match(‘/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.’.
‘(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.’.
‘(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.’.
‘(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/’, $ip) !== 0;
}
[/cc]
Regular Expression obtained here

This will actually work for most situations, but it’s lacking in a few ways. Suppose you want to exclude private or reserved IP addresses. Maybe you want to validate IPv6 addresses too; not just IPv4.

Enter PHP’s Data Filtering Extension. It just works, and you don’t have to worry about maintaining (or properly applying) complex regular expressions.

Read More

PHP and FFMPEG – Performing intelligent video conversion

Whenever I have limited experience with something or just need to do some research, I go straight to StackOverflow.com – a computer programming question and answer site.  Out of the 24 questions I’ve asked on the site, only one has gone unanswered to date, and I’ve gleaned countless other answers from questions others have already asked.  In case you haven’t already guessed, I really like the site.

Not long ago, I needed to convert uploaded video files for a project in PHP.  FFMPEG was a clear and obvious choice for the job, but I had never used it before, so I turned to my favorite resource.  Sure enough, I got enough information in the answers to build a fully working solution.  I posted that solution as an answer to my own question in case anyone else could use it.

Unfortunately, StackOverflow seems to be having trouble displaying my answer, so I’ve reposted it here for anyone who needs it.

Read More