Wow, I am getting so witty with blog titles! But back to the point, since it’s late (again) and I don’t have a lot of time to spend on this. Today is all about String Formatting and Regular Expressions (woohoo!).
String Formatting
- The most basic need of string formatting is with numbers. When you want to display a number, you could display it normally as "12000.5." But it would look so much better with a thousand separator "12,000.5" or even with a certain number of decimals "12,000.50." This is the power of number_format($number, [$decimalPlaces], [$decimalSep], [$thousandSep]). The last two optional variables have to be included together, the function will fail if only one is provided.
- PHP does allow you to set the current formatting rules (locale) for strings, money, and even date and time. This is incredibly useful when you want to target your project to a different country. The function to change the locale is setlocale($category, $locale). As an example, if I wanted to set the money locale to United States mode, I could say setlocale(LC_MONETARY, ‘en_US’); Note: this does not affect number_format().
- Now, using that locale, we can test the money_format($format, $amount) function. This returns a string that formats a number based off a specific monetary locale. The format has a variety of options that we’re not going to cover, but the most important ones are %i (displays international code: "USD 25,000") and %n (displays local code: "$25,000"). You can also add a ".n" after the percent sign (%) where n is the number of decimals to display. So %.2n would display "$25,000.00" Note: even though money_format doesn’t work in Windows or some versions of Unix, the test still requires me to know this.
- Generic string formatting is actually quite simple: take a format and fill in the markers with formatted variables. And the format follows a specific specification:
- Percent sign (%)
- Optional plus sign (+): to display + and – on respective numbers.
- Optional padding character (prefixed with a ‘): what character will pad the number if it is not long enough.
- Optional left alignment (-): normally right justified
- Optional minimum width (digit): if the output is less than this, it will be padded with the padding character.
- Optional precision specifier (prefixed with a . then digit): how many decimal digits are displayed.
- Type specifier:
- s: String
- c: Character (converted from ASCII)
- d: Signed Decimal
- u: Unsigned Decimal
- f: Locale Aware Float
- F: Non-Locale Aware Float
- e: Scientific Notation
- b: Binary
- o: Octal
- x: Hexadecimal with lowercase letters
- X: Hexadecimal with uppercase letters
- This formatting is used with printf($format, $var1, [$var2], …) to display to the screen, sprintf($format, $var1, [$var2], …) to return to a string, and fprintf($format, $var1, [$var2], …) to output to a file. Here is an example:
$a = 150;
-
$b = 2.5;
-
$name = "Bob";
-
$action = "walk";
-
-
printf("%s went for a %s", $name, $action);
-
// Displays: Bob went for a walk
-
-
$output = sprintf("%d divided by %.2f is %d", $a, $b, ($a / $b));
-
// Output is equal to: 150 divided by 2.50 is 60
-
- Just as you can output formatted text, you can also input it. Using sscanf($string, $format), it returns an array with the specified data requested.
$string = “Joe hits 95 per of the time”;
-
$format = "%s hits %d per of the time";
-
$array = sscanf($string, $format);
-
// $array is equal to: ("Joe", 95)
-
Regular Expressions (RegEx)
I’m not going to waste time explaining Regular Expression syntax today, since it would probably take an entire dedicated website to do so. It’s a very complex, pattern-based, searching language designed to extract words/phrases based off of definitions. Here is how PHP implements it:
- Important Note: Regular Expressions are MUCH more computationally intensive than normal find and replace functions in PHP. If you do not need to use RegEx, don’t.
- The basic RegEx search is preg_match($regex, $string, [$array]). It returns 1 if the match is successful and provides any captured sub-patterns in the optional parameter.
- A more advanced version is preg_match_all($regex, $string, $matches), which returns the number of results found and populates the $matches array with the results.
- The replacement function is preg_replace($regex, $replace, $string). This will return the modified string, just as str_replace() works.
Ugh, I’m getting really tired. Luckily I just finished the chapter (skipping a large portion of the RegEx language explanation as I already am well versed in it). Tomorrow looks like it is going to be "Web Programming," a.k.a. GET and POST data. Should be fun!

