Using Regular Expression In PHP – The Basics

 

regular expressions cookbookRegular expressions are a powerful tool for finding, examining and/or modifying text. Regular expressions themselves are, with a general pattern notation almost like a mini programming language, allowing you to define and parse text. They enable you to search for patterns within a string, extracting matches flexible and precise. However, you should note that because regular expressions are more powerful, they also suffer from added overhead and are slower than the more basic string functions. You should make careful consideration and only use regular expressions if you have a particular need.

PHP supports two different types of regular expressions: POSIX-extended and Perl-Compatible Regular Expressions (PCRE). The PCRE functions are more commonly used, are more powerful than the POSIX ones and faster as well

In a regular expression, most characters match only themselves. For instance, if you search for the regular expression “foo” in the string “only a fool does not use regular expressions” you get a match because “foo” occurs in that string. Some characters have a special meaning. For instance, the dollar sign ($) is used to match strings that end with the given pattern. Similarly, a caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string. Characters that match themselves are called literals while characters that have special meanings are called metacharacters.

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Ubuntu 10.10 Maverick Meerkat Said To Be Radical

 

Ubuntu LogoThe new anticipated release of Ubuntu 10.10,  named “Maverick Meerkat” (currently in alpha2) is said to be radical, according to Marc Shuttleworth, chief of Canonical.

Canonical head Mark Shuttleworth mentioned on his blog that the upcoming Ubuntu version 10.10 will be focused on being social and fast. Shuttleworth says “The OS is getting faster and faster when it comes to boot times but the final push remains”. In the mean time, he said that the Netbook Edition of Ubuntu 10.10 will have a revamped UI and will be the fastest booting, fastest network OS for netbooks at this time. Canonical is the company that finances and oversees the Ubuntu project.

Ubuntu 10.10 is being called “Maverick Meerkat”. Meerkats are social creatures by nature and very family oriented and therefore has been adopted for this release. The Ubuntu project has been working very hard to become a viable and open source alternative to leading operating systems such as Mac OS X and  Windows 7.

Ubuntu is probably the most installed Linux distro currently, thanks to many different flavors (Ubuntu, Kubuntu, Xubuntu, Edubuntu, Ubuntu Studio and Ubuntu Netbook Edition). These variations include desktop and server versions including applicable applications and default settings. The last one is for netbooks and started with the amalgamations of Intel’s Moblin (Mobile Linux) into Ubuntu.

Ubuntu 10.10 promises to make everything faster and better than ever before, but ultimately will depend on how good Canonical is at keeping promises. Ubuntu has become a strong competitor and real alternative, and that is especially true for netbooks. For the average consumer, power users and production systems, the benefits come in the form of low cost, good stability and increased security.

Learn more about Ubuntu and the upcoming release.


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Install And Use CouchDB With JSON And Map-Reduce

 

CouchDB is another offspring from the open-source, NoSQL, non-relational databases and is maintained under the Apache Foundation. It differs itself form the likes of MongoDB or Cassandra in that CouchDB is storing data in so called “documents” that are in JSON format, which can be hashes, lists, nested arrays and of course scalar values. This added complexity results in more powerful features, mainly to have a db that is not just a single key/value pair, but it comes at a price of speed reduction.

CouchDB can be a little bit of a pain to install, because it needs a few pre-requisites and they in turn have a few of their own quirks. This outline should help you get CouchDB with all it’s necessities installed. We’ve used MacOS, but you can substitute your OS where applicable.

The CouchDB source code and installer packages are downloadable here. As of this writing, version 0.11.2 is the latest stable version, with 1.0.1 just around the corner. You will also need Spider Monkey, Mozilla’s C implementation of JavaScript.

Installing SpiderMonkey

Once downloaded, extract the tarball and move into the sources folder:

tar -xzvf js-1.8.0-rc1.tar.gz

cd js/src

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Enable Gzip Compression In Apache And Optimize Page Load

 

HTML, CSS and Javascript compression is a simple and effective way to save bandwidth and speed up page load on your site. It’s often overlooked and yet simple to implement, just enable Gzip compression for the right document types and enhance your site’s user experience.

In Apache, we achieve this by enabling content encoding. When a user requests a file like http://www.msn.com/index.html, the browser communicates to a web server, and the conversation goes a something like this:

1. Browser: GET me /index.html
2. Server: Found indes.html, it’s 187KB! Response code is 200 (200 OK) , requested file is coming
3. Browser: 187KB, loading

The actual headers and protocols sent between the two are much more formal (monitor them with HTTPFox, Live HTTP Headers or others if you like.

Now, everything worked just fine in our little scenario, the browser got it’s requested file (index.html) of 187KB in size and probably also loaded include files such as javascript, css and a bunch of images. Especially on lower speed Internet connections, it took a few seconds for the page to load all files.

That’s where compression comes in. Html, javascript and css are plain text and are ideal candidates for compression, while images and videos usually underwent some compression algorithm when saved as jpg, png, avi or flv. Compressing those files would provide very little to no improvement in file size and add processing overhead to the web server. So we want to ensure we exclude those.

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Redis High Speed Storage Or Cache System

 

NoSQL databases are the hype, with MongoBD and CouchDB on the forefront, while Memcache has found a place in many high load web applications during the past few years. Each of these applications has their own, very specific characteristic. MongoDB finds its usage where single key-value pairs are not sufficient, but adds a slight overhead and complexity with its hash table like multi field storage architecture. CouchDB is an ideal candidate where single key-value pair storage engine is sufficient.

And there is Redis, the new kid on the block. Redis is a high speed storage or cache system, much like Memcache on steroids. Redis writes data into memory, which makes it really fast. And in contrast to Memcache, it writes data periodically to disk depending on the amount of data that has changed. Redis is been said to be able to handle in excess of 10’000 reads/writes per second!

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Create Patch Files Using Patch And Diff

 

Using diff to create batch files and subsequently running them against select files is such a convenient way to update, fix or change existing files, but many developers and system administrators simply don’t know much about them.

Here is a quick primer on using diff and apply changes for select files. Use diff –help to check out more options and flags.

NOTE: pay caution when using patching, any mistake or error are executed without warning or undo feature. It’s best to always make a backup of any file or folder that are going to be affected by your patch.

diff

Some useful flags you can specify when comparing files are -b (ignore white space difference), -B (ignore blanc lines), -r (recursive) and -i (ignore case).

Let’s say we need to change the GA analytics code on our pages and that they are unfortunately hard coded. The files are shown below as are the results from diff:

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

How To Clean Up The New Ubuntu Grub2 Boot Menu

 

Clean Up And Modify he new Ubuntu Grub2 boot menu differs quite a bit from the previous version. As Ubuntu nominated the new version 2 of the Grub boot manager as of 9.10, removing of the old problematic menu.lst file.

Ubuntu Grub2 Boot Menu

Ubuntu Grub2 Boot Menu

Grub2 is a leap forward in many ways, and most of the annoyances from menu.lst are gone. Yet, if you don’t clean up old versions of kernel entries, the boot list can quickly get messy and end up in a long list of nonsense. Let’s assume we want to remove the 2.6.32-21-generic boot menu entries. Previously, this meant editing /boot/grub/menu.lst. But with Grub2, we use the package manager to remove the kernel package from our computer, Grub automatically removes those options. Btw. if only one operating system is installed on your computer, you may not see the boot menu at all and have to hold down the SHIFT button on your keyboard while booting up to get the menu to show.

To remove old kernel versions, open up Synaptic Package Manager, found in the System > Administration menu. When Synaptic opens up, type the kernel version that you want to remove into the Quick search text field. The first few numbers should suffice. Then for each of the entries associated with the outdated kernel (e.g. linux-headers-2.6.32-21 and linux-image-2.6.32-21-generic), right-click and choose “Mark for Complete Removal”, then hit “Apply”. These entries will be gone upon the next boot.

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

7 Tips To Improve Web Page Load Time

 

With the increasing focus on Google’s Site Speed Algorithm, the following are 7 tips to improve web page load time, proven techniques well known websites use to boost their site speed.

1. Enable Gzip Compression

While compressing pages adds just a tad to your web server’s overhead, it will reduce bandwidth and transmission time and make pages appear to load faster for your users. Gzip is a open source compression algorithm that can be used to compress the content of your website before your the web server sends data to a client browser. You can learn how to enable Gzip in Apache here.

2. Minify Javascript/CSS

Minify is the process (and software) of removing unnecessary formatting characters and white space from javascript code. The result is smaller files, faster transmission and quicker page loads. You can learn all about minify javascript here.

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Install PHP 5.2 on Ubuntu 10.04 Lucid Lynx

 

There are many reasons why we want to install PHP 5.2 on Ubuntu 10.04 Lucid Lynx, the most prominent is that many web packages are not compatible yet with PHP 5.3. Drupal 6 being a prime example.

But there is no automated method out of the box, and there are now several scripts floating around the Internet that may work or just partially work. A major concern is the ability to update, easily switch to PHP 5.3 when the application is ready and also easily add / remove extensions.

With these considerations in mind, this is the best way to install PHP 5.2 on Ubuntu 10.04 Lucid Lynx:

We will install PHP 5.2 using apt-get and install from Ralp Janke’s repository.

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Best New Chrome Extensions For Developers

 

With increasing popularity in Google’s Chrome browser, so increases the demand for extensions. This is especially true for Developers, as we are used to such a nice variety of high quality tools in Firefox. And the more tools are available on one browser, the more that browser is used in developing web sites, resulting in higher quality web experience for end users on that platform. Btw., did you know that Google Chrome surpasses Safari in US browser market (macstories.net)? The following are arguably the best new Chrome extensions for developers.

6. BuiltWith Technology Profiler

BuiltWith ExtensionBuiltWith is a web site profiler tool, that returns all the technologies it can find on a particular page. BuiltWith extension helps developers, researchers and designers find out what technologies web pages are using and in turn help them to decide what technologies to implement. BuiltWith currently tracks widgets (snap preview), analytics (Google, Nielsen), frameworks (.NET, Java), publishing (WordPress, Blogger), advertising (DoubleClick, AdSense), CDNs (Amazon S3, Limelight), standards (XHTML,RSS), hosting software (Apache, IIS, CentOS, Debian).

5. Session Manager

Session Manager ExtensionWith Session Manager you can quickly save your current browser state and reload it whenever necessary. You can manage multiple sessions, rename or remove them from the session library. Each session remembers the state of the browser at its creation time, i.e the opened tabs and windows. Once a session is opened, the browser is restored to its state.

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Testing Memcached Using Telnet Commands

 

Troubleshooting memcached is not so transparent as some other technologies, but testing memcached using telnet commands can give us quite some insight on what’s happening under the hood.

Following is a short list of useful commands to inspect a running memcached instance.

How to find the IP address and port to connect:

ps aux | grep memcached will give us the process running memcached, with listening ip address and port. If this command does not yield any results, you likely not running the daemon and need to start it up first.

We can now connect using this info:

telnet 127.0.0.1 11211 (replace your IP address and port)

Supported Commands:

The following is a list of the most important memcached commands. For a more complete list of supported commands, check out the memcached wiki document.

Continue reading…


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Burn vob Files To DVD On Mac / Linux

 

If you’ve got some .vob files and wonder how to get those onto a DVD and actually play it on your DVD player, you may get or already have some strange results.
The files might just be listed on the DVD, without the disc actually playing the movie as a DVD, or the disc may play the first vob section, then the second has no sound. And there is a plethora of issues and scenarios, just not the one you were expecting.

There are multiple ways to do it right and to achieve what you want. Here are two distinct ones:

1) Get a program called Roxio Toast 10 Titanium and click on create DVD. Then simply drag your files from left to right, follow the instructions and hit “Burn”. That’s it, it’s that easy if you can fork out $75.-. And the program can do much more than just that, check it out.

2) Save some money, have fun (maybe) and learn in the process.

- Hop on the terminal of you Mac and ensure that you have hdiutil. Type hdiutil info and you’ll know what version you have, which should be close to framework 283.

- Create an ISO which you can then burn to the DVD:  hdiutil makehybrid -udf -udf-volume-name DVD -o DVD Directory_of_DVD where DVD is the name of the iso file. This can take several minutes, depending on the length and size of your movie and the speed of your Mac.

- Mount the iso on your Mac by double clicking it and verify that it works as intended.

- Unmount it and burn it to DVD with Disk Utility or your favorite burner.


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Free Version Of Microsoft Word With Office 2010

 

Microsoft is releasing a new edition of Office to businesses this Wednesday and for the first time it’s adding free versions online versions of MS Word and other programs that work in a Web browser. It’s expected that these free apps will have fewer features than the desktop versions.

MS Office 2010 marks a milestone in the company’s efforts to stay abreast with an shift within the software industry that moves away from programs running locally on computers to free services that can be accessed from any browser over the Internet. Microsoft must walk a fine line and be careful not to make the free applications too appealing as to impact its lucrative software business negatively.
Software accounted for 29 percent of Microsoft’s revenue and 51 percent of its operating income in the most recent quarter.

In contrast to it’s competitor, 4 percent of companies use Google Apps today, according to Forrester Research.

In addition, the Outlook e-mail program will be able to pull in information from users’ outside social networks, such as Facebook and LinkedIn. It also adds new features to tame the ever-growing number of messages in the inbox, including a way to group all replies to a single thread under one line.


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

How to find duplicate rows in a MySQL database table

 

I’ve been asked the question “How can I return duplicate rows only from a MySQL db table” so many times already, that I’ve decided to post it here in a short article.

It is not something intuitive or readily available (at least it seems), but the solution is short and very simple.

While this query:

SELECT DISTINCT column1
FROM table1

gives us all records without the duplicates, this one returns only the duplicate ones:

SELECT DISTINCT column1
FROM table1
GROUP BY column1
HAVING COUNT(column1) > 1

And by increasing the having count, you can retrieve records with multiple occurrences.









  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark

Track And Parse Twitter Messages Stream

 

Ever wanted to listen, track and parse tweets from a Twitter stream from an individual user? It’s quite an easy task, if you know the right URL’s to parse. Curl is a great little tool to get sources from almost any web resource and Curl has roots in Linux command like and PHP, just to name a couple.

Let’s look at how this would work by pulling CNN’s breaking news feed as JSON:

curl http://twitter.com/status/user_timeline/cnnbrk.json

or XML:

curl http://twitter.com/status/user_timeline/cnnbrk.xml

That will give you the last 20 tweets in either JSON or XML format.

An even more interesting option is to get messages where you have been mentioned. It’s a bit more complicated, as we need to supply login credentials, but no rocket science either:

curl -u “username:password” http://www.twitter.com/statuses/mentions.json (or .xml if you prefer)

That’ll give you the last few tweets mentioning the user supplied with the curl command. Now all you got to figure out is how to parse the messages and plug them into your application.


  • Digg
  • Facebook
  • Twitter
  • StumbleUpon
  • Windows Live Favorites
  • Yahoo Buzz
  • Share/Bookmark