Sunday, July 5, 2009

string wordwrap ( string source [, int width [, string break [, boolean cut]]])

string number_format ( float number [, int decimal_places])

string number_format ( float number, int decimal_places, string decimal_point, string thousands_seperator)

Number_format() is a remarkably helpful function that takes a minimum of one parameter, the number to format, and returns that same number with grouped thousands. There are two function prototypes for number_format() as you either pass it one, two, or four parameters - passing it one or two fits the first prototype, and passing four fits the second.

So, if you pass number_format() a parameter of "1234567", it will return "1,234,567". By default, number_format() rounds fractions - 1234567.89 becomes 1,234,568. However, you can change this by specifying the second parameter, which is the number of decimal places to include. Parameter three allows you to choose the character to use as your decimal point, and parameter four allows you to choose the character to use as your thousands separator. Here is how it all looks in PHP:
$num = 12345.6789;
$a = number_format($num);
$b = number_format($num, 3);
$c = number_format($num, 4, ',', '.');
?>

After running that script, $a will be set to 12,346, $b will be set to 12,345.679, and $c will be set to 12.345,6789 (periods used to separate thousands, and commas used for the decimal point, east European-style).

As you can imagine, number_format() is incredibly useful when it comes to formatting money for checkout pages in shopping baskets, although it is useful anywhere you need to represent large numbers - adding a thousand separator invariably makes things easier to read.

Wrapping your lines

string wordwrap ( string source [, int width [, string break [, boolean cut]]])

Although web pages wrap text automatically, there are two situations when you might want to wrap text yourself:

*

When printing to a console as opposed to a web page, text does not wrap automatically. Therefore, unless you want your users to scroll around a lot, it is best to wrap text for them.
*

When printing to a web page that has been designed to exactly accommodate a certain width of text, allowing browsers to wrap text whenever they want will likely lead to the design getting warped.

In either of these situations, the wordwrap() function comes to your aid. If you pass a sentence of text into wordwrap() with no other parameters, it will return that same string wrapped at the 75-character mark using "\n" for new lines. However, you can pass both the size and new line marker as parameters two and three if you want to, like this:
$text = "Word wrap will split this text up into smaller lines, which makes for easier reading and neater layout.";
$text = wordwrap($text, 20, "
");
print $text;
?>

Running that script will give you the following output:
Word wrap will split
this text up into
smaller lines, which
makes for easier
reading and neater
layout.

As you can see, wordwrap() has used
, a HTML new line marker, and split up words at the 20-character mark. Note that wordwrap() always pessimistically wraps words - that is, if you set the second parameter to 20, wordwrap() will always wrap when it hits 20 characters or under - not 21, 22, etc. The only exception to this is if you have words that are individually longer than 20 characters - wordwrap() will not break up a word, and so may return larger chunks than the limit you set.

If you really want your limit to be a hard maximum, you can supply 1 as a fourth parameter, which enables "cut" mode - words over the limit will be cut up if this is enabled. Here is an example of cut mode in action:
$text = "Micro-organism is a very long word.";
$text = wordwrap($text, 6, "\n", 1);
print $text;
?>

That will output the following:
Micro-
organi
sm is
a very
long
word.

Finding a string within a string

Strpos(), and its case-insensitive sibling stripos(), returns the index of the first occurrence of a substring within a string. It is easier to explain in code, so here goes:
$string = "This is a strpos() test";
print strpos($string, "a") . "\n";
?>

That will return 8, because the first character in "This is a strpos() test" that is a lowercase A is at index 8. Remember that PHP considers the first letter of a string to be index 0, which means that the A strpos() found is actually the ninth character.

You can specify whole words in parameter two, which will make strpos() return the first position of that word within the string, for example strpos($string, "test") would return 19 - the index of the first letter in the matched word.

If the substring sent in parameter two is not found in parameter one, strpos() will return false. Consider this script:
$string = "This is a strpos() test";
$pos = strpos($string, "This");
if ($pos == false) {
print "Not found\n";
} else {
print "Found!\n";
}
?>

If you try executing that, you will find that it outputs "Not found", despite "This" quite clearly being in $string. Is it another case sensitivity problem? Not quite. This time the problem lies in the fact that "This" is the first thing in $string, which means that strpos() will return 0. However, PHP considers 0 to be the same value as false, which means that our if statement cannot tell the difference between "Substring not found" and "Substring found at index 0" - quite a problem!

Luckily, PHP comes to the rescue with the === operator, which, if you recall, means "is identical to", which means $pos must be equal to false and of the same type as false (boolean). If the "This" is found in $string, strpos()will return 0, but it will be of type integer . If we change our if statement to use === rather than ==, PHP will check the value of 0 and false and find they match (both false), then check the types of 0 and false, and find that they do not match - the former is an integer, and the latter is a boolean.

So, the corrected version of the script is this:
$string = "This is a strpos() test";
$pos = strpos($string, "This");
if ($pos === false) {
print "Not found\n";
} else {
print "Found!\n";
}
?>

Now, consider this next script, which tries to match the "i" in "is":
$string = "This is a strpos() test";
$pos = strpos($string, "i");
if ($pos === false) {
print "Not found\n";
} else {
print "Found at $pos!\n";
}
?>

The problem there is that strpos() matches the first "i" it comes across, which will be in "This". Fortunately there is a third parameter to strpos() that allows us to specify where to start from. As the "i" in "This" is at index 2, we just need to specify one place after that (3) as the start position for strpos(), and it will report back the next "i" after it. For example:
$string = "This is a strpos() test";
$pos = strpos($string, "i", 3);
if ($pos === false) {
print "Not found\n";
} else {
print "Found at $pos!\n";
}
?>

This time that will print "found at 5!", which is the position of the "i" in "is".

Padding out a string

string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])

Next up, str_pad() makes a given string (parameter one) larger by X number of characters (parameter two) by adding on spaces. For example:
$string = "Goodbye, Perl!";
$newstring = str_pad($string, 10);
?>

That code would leave " Goodbye, Perl! " in $newstring, which is the same string from $string except with five spaces on either side, equalling the 10 we passed in as parameter two.

Str_pad() has an optional third parameter that lets you set the padding character to use, so:
$string = "Goodbye, Perl!";>
$newstring = str_pad($string, 10, 'a');
?>

That would put "aaaaaGoodbye, Perl!aaaaa" into $newstring.

We can extend the function even more by using it is optional fourth parameter, which allows us to specify which side we want the padding added to. The fourth parameter is specified as a constant, and you either use STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH:
$string = "Goodbye, Perl!";
$a = str_pad($string, 10, '-', STR_PAD_LEFT);
$b = str_pad($string, 10, '-', STR_PAD_RIGHT);
$c = str_pad($string, 10, '-', STR_PAD_BOTH);
?>

That code will set $a to be "----------Goodbye, Perl!", $b to be "Goodbye, Perl!----------", and $c to be "-----Goodbye, Perl!-----", as expected.

Note that HTML only allows a maximum of one space at any time. If you want to pad more, you will need to use " ", the HTML code for non-breaking space.

Parsing a string into variables

void parse_str ( string input [, array store])

Previously we looked at a handful of the variables set for you inside the superglobal arrays, of which one was QUERY_STRING. If you recall, this is the literal text sent after the question mark in a HTTP GET request, which means that if the page requested was "mypage.php?foo=bar&bar=baz", QUERY_STRING is set to "foo=bar&bar=baz".

The parse_str() function is designed to take a query string like that one and convert it to variables in the same way that PHP does when variables come in. The difference is that variables parsed using parse_str() are converted to global variables, as opposed to elements inside $_GET. So:
if (isset($foo)) {
print "Foo is $foo
";
} else {
print "Foo is unset
";
}

parse_str("foo=bar&bar=baz");

if (isset($foo)) {
print "Foo is $foo
";
} else {
print "Foo is unset
";
}
?>

That will print out "Foo is unset" followed by "Foo is bar", because the call to parse_str() will set $foo to "bar" and $bar to "baz". Optionally, you can pass an array as the second parameter to parse_str(), and it will put the variables into there. That would make the script look like this:
$array = array();

if (isset($array['foo'])) {
print "Foo is {$array['foo']}
";
} else {
print "Foo is unset
";
}

parse_str("foo=bar&bar=baz", $array);

if (isset($array['foo'])) {
print "Foo is {$array['foo']}
";
} else {
print "Foo is unset
";
}
?>

That script outputs the same as before, except that the variables found in the query string are placed into $array. As you can see, the variable names are used as keys in the array and their values are used as the array values.

Regular expression syntax examples

In order to give you a quick reference to the different patterns and what they will match, here's a comprehensive table of all we've covered. Column one contains example expressions, and column two contains what that expression will match.

Expr


Will match...

foo


the string "foo"

^foo


"foo" at the start of a line

foo$


"foo" at the end of a line

^foo$


"foo" when it is alone on a line

[Ff]oo


"Foo" or "foo"

[abc]


a, b, or c

[^abc]


d, e, f, g, h, etc - everything that is not a, b, or c (^ is "not" inside sets)

[A-Z]


any uppercase letter

[a-z]


any lowercase letter

[A-Za-z]


any letter

[A-Za-z0-9]


any letter of number

[A-Z]+


one or more uppercase letters

[A-Z]*


zero or more uppercase letters

[A-Z]?


zero or one uppercase letters

[A-Z]{3}


3 uppercase letters

[A-Z]{3,}


a minimum of 3 uppercase letters

[A-Z]{1,3}


1-3 uppercase letters

[^0-9]


any non-numeric character

[^0-9A-Za-z]


any symbol (not a number or a letter)

Fo*


F, Fo, Foo, Fooo, Foooo, etc

Fo+


Fo, Foo, Fooo, Foooo, etc

Fo?


F, Fo

.


any character except \n (new line)

\b


a word boundary. E.g. te\b matches the "te" in "late", but not the "te" in "tell".

\B


a non-word boundary. "te\B" matches the "te" in "tell" but not the "te" in "late".

\n


new line character

\s


any whitespace (new line, space, tab, etc)

\S


any non-whitespace character

Checking whether a function is available

bool function_exists ( string function_name)

If you're working with functions that are not part of the core of PHP, that is, functions that are from an extension that needs to be enabled by users, it's a smart move to use the function_exists() function. This takes a function name as its only parameter, and returns true if that function (either built-in, or one you've defined yourself) is available for use. Note that it only checks whether the function is available, not whether it will work - your system may not be configured properly for some functions.

Author's Note: If you ever want to know whether you have a function available to you, use the function_exists() function. This takes one string parameter that is the name of a function, and returns true if the function exists or false if it does not. Many people use function_exists() to find out whether they have an extension available, by calling function_exists() on a function of that extension. However, this is accomplished much more easily with the function extension_loaded() function covered later.