Categories
PHP

Decoding Google’s Tweet With PHP

This is something for fun — nothing serious. It just happened that Google just sent a mysterious Tweet around 2 hours ago from now with an image with of the Google home page featuring an UFO. The UFO was seen pulling an O out of the google logo. But what the heck does that tweet mean ?

1.12.12 25.15.21.18 15 1.18.5 2.5.12.15.14.7 20.15 21.19 was the tweet.

Well, it’s nothing tough. Every number separated by a dot or space corresponds to the alphabet. So, I wrote a PHP script to decode this mysterious tweet and finally found the meaning:

So, it meant ” all your o are belong to us ” 😉
Sounds a bit odd ? Or wrong in grammar ? yeah, still, that’s the message. It is confirmed on the twitpic image.

And here’s the source code —

It was quite a bit of fun decoding the message with PHP. Well, I do agree that there was no need to use PHP here. But why do the hard work from memory when you have automation tools at your hand 😉 ?

Categories
PHP

PHP: Counting Occurrences with PCRE

While playing with PHP, I have found a fact that we can determine how many times a string occurs inside another string quite easily by using PCRE. In fact, we can use preg_match_all() to match a string (instead of a complex pattern) to another string and get the matches as an array. Then we count() the array’s first key to know how many times the string has been found in the other one.

We feed preg_match_all() a solid string as a pattern as the first parameter. And it’d do the job for us.

Here’s the code snippet:

In the above example, we add an “i” modifier for case insensitive matching. If you want it to be a case sensitive matching, just ignore the “i” modifier.

Results:

Additionally this method can be used instead of strpos() when we try to find if a string exists in another.

Categories
PHP

Interactive PHP Shell

You might have seen the cool interactive shell of python. And/Or the IDLE that comes with Python for Windows and as an add on for Python on Linux ? That’s really handy for prototyping. Isn’t that ? Well, I always counted the interactive shell of python as one of the many key strengths of Python over PHP. I knew that PHP did have interactive mode as well, but that didn’t look pretty to me. Well, I was using Windows then.

A long time has passed meanwhile and today, while trying to find out something to do in my leisure, guess what, I stumbled upon PHP again. I was using the Python shell that I launched from my Ubuntu’s “Applications > Programming ” menu. I was playing with the base64 module of Python when I came to think about the base64_encode() function of PHP and immediately chose to experiment with PHP leaving Python.

I was going to write a script when suddenly an idea struck me. Why not try the interactive mode in Linux? Does it have anything different in Linux ?

I opened up a terminal and typed in:

Oh, I forgot to tell you that, I did have php5-cli package installed on my Linux. To try php from command line, you must have this package installed. To install it, use your default package manager or type on your Ubuntu terminal:

In case of some other distro of Linux, follow your manual to install php5-cli.

Now that I had php command line interpreter installed, I got this message:

Wow… !! Did you see that “php >” line ? It’s a real shell prompt then ? I didn’t see that on Windows.

Still more surprises were left for me. I had to spend a bit of time to figure out how it works. It’s very plain. You don’t need the <?php and ?> delimiters. Just complete a pure php statement and press enter. Like this:

PHP has been always very well accepted for it’s flexibility and I don’t miss that in the interactive shell either. 😉

You can easily expand php statements over multiple lines:

Here, you can see that I have spread the following statement into three lines:

The first line contains — echo.
The second line has the string — “hello”.
And the final terminates and completes the statement with the semicolon.

It’s simple. Isn’t it ?

Let’s see some examples:

Cool, eh ? 😀

Now, if you are Using Ubuntu or any other distro with Gnome desktop, launch the menu editor and add a new item into the Programming sub menu.

On Ubuntu:
— Right click on the main menu bar.
— Click “Edit Menus”.
— In the “Menus” section, select “Programming”.
— Click “New Item”.
— A “Create Launcher” dialog box will pop up.
— Fill it as below:
Type: Application in Terminal
Name: PHP Shell
Command: php -a
Comment: Interactive PHP Shell
— Press “OK”.
— Press “Close” on the “Main Menu” dialog box.

You’re finally done adding a fancy PHP shell directly to your main menu. You can of course run PHP shell from terminal just by typing:

But having an item in the main menu does help, doesn’t it ? 😉

Have fun, have a nice time !! 😀