Tag Archives: php

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. Read more »

Hot To Install Memcache And PHP Client On Mac Snow Leopard

I recently installed the memcached daemon on my MacBook Pro, incuding the necessary PHP client for development purposes. I just prefer to work locally instead of using a VM running Linux. And the process is actually quite simple and straight forward. Please note, I have included both clients, the old standard one and the newer PECL extension, because I deal with different applications and also lots of people seems to get confused when they install one version and their memcache classes cannot get instantiated and throw errors. So, if in doubt, just install both.

These are the five (four if you know which extension you want) components needed:

- libevent (requred library for memcached)

- memcached daemon

- libmemcached (required library for the php client)

- php extension (standard)

- php extension (PECL)

Now open your terminal and off we go: Read more »

How To Add PHP Mcrypt Module On Snow Leopard 10.6

These instructions assume that you have a working Apache 2.2 / PHP 5.3 in place and want to add the php mcrypt module. It will work as a fresh install, but keep in mind that additional configuration steps after these instructions are necessary to get your webserver working properly. Those additional steps are omitted here, as there are countless resources available on the Internet.

1. If you don’t already have the mcrypt module (in /usr/lib), download the libmcrypt source code from sourceforge here. Then extract the downloaded file in a Terminal and move inside the created directory. (cd libmcrypt-2.5.8 in my case)

2. Execute the following lines in one command in your Terminal, if you have a 64bit version of Apache/PHP:

CFLAGS=”-arch x86_64″
CXXFLAGS=”-arch x86_64″
./configure –disable-posix-threads

(for 32 bit versions: ./configure –disable-posix-threads)

Read more »

How to install MySQL Server, PHP and Apache on a Mac

These instructions lead you thru the installation of the latest LAMP Stack on MacOS X Snow Leopard.

1 – Download the installation image from MySQL website here. Then double-click to mount and open the disk image.

2 – Install MySQL Server by double clicking the package “mysql-5.1.*****.pkg” and follow the menu, accepting the default values, unless you want to change something and know exactly what you’re doing.

3 – Install MySQL Startup Item by double-clicking the package “MySQLStartupitem.pkg” and follow the menu.

4 – Install MySQL Preference Pane by double-clicking the file “MySQL.prefPane” and follow the menu. This item will simplify the management of your SQL Server. You can now use the “System Preferences” panel to start and stop the database server.

5 – Enable the php module in your apache config file. You might know that Snow Leopard already ships with Apache 2.2 and PHP 5.3, but it needs a couple of tweaks to make it work smoothly. So, open /etc/apache2/httpd.conf and search for “php5_module”. Remove the comment (#) in front of the line, save and close the file, then restart apache (sudo apachectl restart)

Read more »

Install JSON PHP Extension on CentOs / RedHat

I had numerous requests for info and questions relating to JSON extension in CentOS. To enable these functions in RedHat and CentOs 5, the process is really simple and fast.

NOTE: As of PHP 5.2, json extension is now standard. If you’re running PHP 5.2 or later, or like to upgrade instead, you can skip this!

  1. Ensure you have the necessary dependecies (php, php-pear, php-devel, gcc, make):
    • $ sudo yum install gcc make
    • $ sudo yum install php php-pear php-devel
  2. Use PECL (PHP Extension Community Library) to download the json package:
    • $ sudo pecl download json
  3. Use PEAR (PHP Extension and Application Repository) to extract and install the extension:
    • $ sudo pear install json-1.2.1.tgz
  4. Create a file in /etc/php.d called “json.ini”, and add the following lines:
    • ; php-json extension
    • extension=json.so
  5. Restart apache (gracefully if you’re running a live site:
    • $ sudo service httpd restart (apachectl graceful)
  6. Check for availability by creating an info.php file in the web root with the following line:
    • <?php phpinfo(); ?>
  7. Load info.php in your browser and check for JSON. You now should be all set, but if it doesn’t appear, verify all of the above steps very carefully.

Nginx and memcached module

Memcache is traditionally used as a module inside server side scripts, such as PHP, ASP, ColdFusion and others. And it’s doing a terrific job, as long as it’s implemented correctly.

But if we look under the hood of the actual Memcache application, and I’m not talking about the PHP or ASP extension, but rather the executable that’s running as a daemon under linux, for example, it is a rather simple database like application running in memory. Now there are two basic actions that need to be performed to use it, one is writing info into memcache, the other is reading it. In a typical scenario, there are many reads for one write, that’s the whole point, isn’t it. But what if we can isolate the reading from the server side scripts, and let a small high speed module do that for us.

Read more »

Eclipse Ganymede and PDT 2.0 for PHP Development

With the release of Eclipse Ganymede, the IDE has become even better. I remember using Zend Studio for Eclipse 6.0.1 for about two weeks, after giving up and moving over to Netbeans 6.5. Too many issues with the debugging part, constant exceptions and a sizable slow down when working on web pages just got me too frustrated.

Now Ganymede is out for a while, but I just haven’t given it a change until now. And I’m positively surprised. While still fairly slow and lagging wile editing html/css content, gone are the countless exceptions, the cumbersome, seemingly endless installation procedure and also xDebug integrates quite nicely with the IDE. There are still a few caveats during the install, especially on a 64 bit environment.

Read more »