Regular Expression for URLs using ereg_replace

Ever needed a Regular Expression to parse URLs in a body of text. The beauty with this expression is its flexibility, it will handle a http or https url with or without www and it’s fine with tinyurls.

[sourcecode language="php"]
// $message contains a body of text with non html URLs
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $message);
[/sourcecode]

$subject will be the body of text containing flat text urls. $text will now contain our information.

[sourcecode language="php"]
// Output New formatted content with links.
echo $text;
[/sourcecode]

Now our content is parsed with URLs.

Done!

PHP’s Memory Usage

This is a handy little Snippet that goes well with PHP Microtime. Not only are we concerned with our scripts execution time but we also want to make sure we are not using an exorbitant amount of memory in the process.

At the top of your script lets place the code that will start PHP’s Micro time.
[sourcecode language="php"]
$time_start = microtime(true);
[/sourcecode]

At the bottom of your page, just before the “ ” tag we will put the Code that will calculate the execution time as well as how much memory was used to execute the script.

[sourcecode language="php"]
// RENDER TIME
$time_end = microtime(true);
$time = $time_end – $time_start;

echo &quot;Loaded in \n&quot;;
echo round($time, 3).&quot; Seconds &lt;br /&gt;&quot;;
echo ‘Peak: ‘ . number_format(memory_get_peak_usage(), 0, ‘.’, ‘,’) . &quot; bytes\n &lt;br /&gt;&quot;;
echo ‘End: ‘ . number_format(memory_get_usage(), 0, ‘.’, ‘,’) . &quot; bytes\n&quot;;
[/sourcecode]

number_format() simply formats the number out put to look something like this “111,964″ instead of “111964″.

memory_get_peak_usage() is going to return a value based on the peak memory used during the execution. This is more so were we want to see improvements made.

memory_get_usage() will return the amount of memory used once the script is done processing, basically the amount currently used once the crunching is finished.

It’s a good idea to think about memory usage when you are making web applications as this can impact your hosting.

Say you have a VPS server with 256mb of ram but the foot print of each user is 25mb you can can only have about 10 people accessing your site before you running server errors exceeding memory limits. Any work involving processing of images a lot of data manipulation can eat up memory in a hurry.

Work with RSS feeds using PHP and cURL

cURL or Client URL Library is a very powerful tool, and its something that i recently had to use while working with two APIs one for Unfuddle and one for HelpSpot.

PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

These functions have been added in PHP 4.0.2.

You can interact with most APIs using cURL, at first it seams intimidating but once you start to use it you become comfortable with the set up.

Lets get started by parsing an RSS feed.

Read More

Sequel Pro on Dreamhost

If you are doing any work with a database you probably have used phpMyAdmin. It is an excellent tool, and for a lot of tasks I prefer to use it.

But when it comes down to QA or debugging of data on an established database I like to use an application. For the Mac I use Sequel Pro. Don’t let the Pro fool you though, it is a free application but rely on Donations.

I would offer the MySQL Query Browser as a PC alternative but it has since been redeveloped into a workbench. I have not spent much time getting to know it so for now I will focus on the Mac.

Since I use Dreamhost I will lay out the steps needed to connect to your Database.

Other hosts might allow something similar but unless they offer an external connection like mysql.domain.com or by IP then you wont be able to use localhost.

Setup your User:

To gain access You need to add your external IP to the MySQL user by going into the MySQL Goodies, Click on your database user name to get to the property’s. Under “Allowable Hosts” enter you IP address. It will also be listed beside the text box. But you can use IP Chicken if you are unsure.

Open up Sequel Pro and fill in the blanks.

When you open up Sequel Pro make sure that you have a Standard connection selected. Give the connection a name. enter your Dreamhost mysql address, User-name, Password, leaving the port as default (3306).

Connect and everything should work, you can now select a database from the drop down on the left. If you get a connection error then check your log in credentials, make sure your External IP is correct, you may also need to wait a few min for Dreamhost to add your IP to their system.

Php Microtime

This is a handy bit of code that will help when optimizing your script. Most MVC or NonMVC frameworks have a benchmark feature but if you are on your own this will do just fine.

The PHP microtime() function will return the current Unix timestamp.

There isn’t much to this script, we are going to assign $time-start the start time. Then below your code to time place the $time_end.

A little simple math will tell us the difference. $time-start minus $time-end will give us the execution time.

If we were to use the raw result we would see something like this: 0.0562338829041 Seconds.

Its a bit to detailed do be of any use. We can use the PHP Function round() to round the number to 3 decimal places.

[sourcecode language="php"]
$time_start = microtime(true);

// Code Here
for ( $counter = 1; $counter &lt;= 1000000; $counter += 1) {
$counter;
}

$time_end = microtime(true);
$time = $time_end – $time_start;

echo &quot;Loaded in &quot;.round($time, 3).&quot; seconds\n&quot;;
[/sourcecode]

That’s it!

Php switch statement

A switch statement is like a series of if statement. We are comparing a value with many other values.

Pretend we have a script and want to greet our users.

[sourcecode language="php"]
<?php

$user_name = ‘adam’;

switch( $user_name ) {
case "dave":
echo ‘Hello Dave.’;
break;
case "dan":
echo ‘Hello Dan.’;
break;
case "bill":
echo ‘Hello Bill.’;
break;
case "adam":
echo ‘Hello Adam.’;
break;
default:
echo ‘Hello Guest’;
break;
}
?>
[/sourcecode]

The above example is not the best use for a switch statement, but it gets the point across.

In the past I have used them to read a URL path for information or in functions for processing images based on the file extension.

Read more about the PHP Switch statement.