Using regular expressions to extract content

Tuesday, April 22nd, 2008

PHP provides a number of really neat regular expression functions. You can find the list of the regex function at the PHP site.

But the one that I’ve had most fun with is the preg_match_all() function which I’ve been using to do content extraction from an HTML page.

(more…)

Parsing a URL querystring into variables in PHP

Thursday, April 17th, 2008

It’s common knowledge you can read variable from a URL querystring by using $_GET array in PHP, but that’s only possible if the URL is “executed” in the browser.

For those who might not know, querystrings are those variable-value pairs that appears behind ‘?’ of a URL. For example,

http://www.mysite.com/index.php?variable1=1&variable2=1

gives the querystring “variable1=1&variable2=1″.

What happens if you end up reading a URL from, say, a text file and you want to parse the querystring? In this case, the $_GET won’t work. So instead, PHP provides a function called parse_str(), which will convert the querystring into actual PHP variables within the scope of the code.

(more…)

Checking a string is alphanumeric in PHP

Friday, April 11th, 2008

There may be times when you need a way to check that a string only contains alpha-numeric characters. That means only alphabets A-Z nad numbers 0-9.

Most PHP-ers will probably use regular expressions and the function preg_match(). I will admit that regular expressions is really powerful, but it’s a real pain to learn and master. Fortunately PHP provides another function you can use.

Check out ctype_alnum() function.

(more…)

PHP Operators: Assignment

Monday, February 11th, 2008

The main assignment operator is ‘=’, which basically assigns a value on the right to a variable on the left. If you think this is the same as an equal sign in mathematics, don’t! Think of it as a ‘assigned to’ so whatever is on the right is assigned to the left.

Here’s a simple example:

(more…)

PHP Operators: Maths

Thursday, January 31st, 2008

There are various kinds of operators used in PHP, but we’re going to start with the most basic and commonly known operator - the Math operators.

What are operators? They are basically a special set of characters (or symbols, if you like) which tells PHP to perform a certain operation. For example, the ‘+’ (plus sign) is used for arithmetic adding of 2 values or variables.

Now you know why I’m starting with the Math operators, cos anyone who has had any basis schooling in Mathematics will be familiar with the Math operators.

(more…)

Using comments in your PHP scripts

Wednesday, January 23rd, 2008

Adding comments to PHP code is a very useful practice. Comments allows you to add some descriptive text that you can use to document or remind you on what the various parts of the PHP script is doing.

Comments are not sent to the browser, so it won’t be visible in the actual page you load in your browser, but it will be visible in your text editor you use for writing your PHP script.

(more…)

Google