Zend PHP5 certification Exam study review 1: Strings and Regular Expressions

In this post, I list some of the aspects that you may need to refresh your memory on PHP5’s String and Regular Expressions feature. String and Regx are very important for any web development language, so you should have great grasp on them before you take the Exam. The funcitons listed here are no guarantee will be tested in the exam, they are just what I thought you should know them. Of course, these are just a fraction of the functions on php.net; I encourage you visit php official manual often!int strlen ( string $string )

string strtr ( string $str, string $from, string $to )
string strtr ( string $str, array $replace_pairs )

int strcmp ( string $str1, string $str2 )
int strcasecmp ( string $str1, string $str2 )
Note: PHP first transparently converts strings to their numeric equivalent; use “===” in the IF logic

int strpos ( string $haystack, mixed $needle [, int $offset] )
int stripos ( string $haystack, string $needle [, int $offset] )
int strrpos ( string $haystack, string $needle [, int $offset] )

string strstr ( string $haystack, string $needle )
string stristr ( string $haystack, string $needle )

int strspn ( string $str1, string $str2 [, int $start [, int $length]] ) whitelist
int strcspn ( string $str1, string $str2 [, int $start [, int $length]] ) blacklist

mixed str_replace ( mixed $search, mixed $replace, mixed $subject [, int &$count] )
mixed str_ireplace ( mixed $search, mixed $replace, mixed $subject [, int &$count] )
mixed substr_replace ( mixed $string, string $replacement, int $start [, int $length] )
The result string is returned. If string is an array then array is returned

string substr ( string $string, int $start [, int $length] )

string number_format ( float $number [, int $decimals [, string $dec_point, string $thousands_sep]] ) not locale-aware
string money_format ( string $format, float $number ) locale aware
The money_format() function is not available onWindows, as well as on some variants of UNIX.
string setlocale ( int $category, string $locale [, string $…] )
string setlocale ( int $category, array $locale )

Printf Family print(), sprintf(), vprintf(), sscanf(), fscanf()
—————————————————
—————————————————
int printf ( string $format [, mixed $args [, mixed $…]] )

commonly-used type specifiers:
% – a literal percent character. No argument is required.
b – the argument is treated as an integer, and presented as a binary number.
c – the argument is treated as an integer, and presented as the character with that ASCII value.
d – the argument is treated as an integer, and presented as a (signed) decimal number.
e – the argument is treated as scientific notation (e.g. 1.2e+2).
u – the argument is treated as an integer, and presented as an unsigned decimal number.
f – the argument is treated as a float, and presented as a floating-point number (locale aware).
F – the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3.
o – the argument is treated as an integer, and presented as an octal number.
s – the argument is treated as and presented as a string.
x – the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
X – the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

Perl-compatible Regular Expressions
Delimiters
By convention, the forward slash is used
Metacharacters
\d Digits 0-9
\D Anything not a digit
\w Any alphanumeric character or an underscore (_)
\W Anything not an alphanumeric character or an underscore
\s Any whitespace (spaces, tabs, newlines)
\S Any non-whitespace character
. Any character except for a newline
Quantifiers
? Occurs 0 or 1 time
* Occurs 0 or more times
+ Occurs 1 or more times
{n} Occurs exactly n times
{,n} Occurs at most n times
{m,} Occurs m or more times
{m,n} Occurs between m and n times
Sub-Expressions
compare this to the “range” e.g. [1-9]
PCRE – Pattern Modifiers
– i – Case insensitive search
– m – Multiline, $ and ^ will match at newlines
– s – Makes the dot metacharacter match newlines
– x – Allows for commenting
– U – Makes the engine un-greedy
– u – Turns on UTF8 support
– e – Matched with preg_replace() allows you to call

Functions:
int preg_match ( string $pattern, string $subject [, array &$matches [, int $flags [, int $offset]]] )
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern,
$matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

int preg_match_all ( string $pattern, string $subject, array &$matches [, int $flags [, int $offset]] )

mixed preg_replace ( mixed $pattern, mixed $replacement, mixed $subject [, int $limit [, int &$count]] )
to reuse captured subpatterns directly in the substitution string by prefixing their index with a dollar sign

Just like with str_replace(), we can pass arrays of search and replacement arguments; however, unlike str_replace(), we can also pass in an array of subjects on which to perform the search-and-replace operation. This can speed things up considerably, since the regular expression (or expressions) are compiled once and reused multiple times.

5 Comments - Leave a comment
  1. […] pat pateiksiu nuorodą į vieno užsieniečio blog’ą, kuriame jis parašė straipsnių ciklą apie pasiruošimą sertifikato laikymui. Blog’e yra […]

  2. Hesi exam says:

    Try to compile revision summaries throughout the course, not just at the end of it.

  3. Thanks for sharing you knowledge and refreshing our mind with this information. I think you have done exceptionally good job to give information what the students want.

  4. […] mir auf jeden Fall die Mock Tests von php|architect. Leute mit ohne Geld sollten sich mal die ganze Serie auf dem readtheweb Blog reinziehen (oder am Wettbewerb unten teilnehmen). Carlton Dickson hat eine Auflistung von weiteren […]

  5. Nicolo says:

    Hello,

    I have found a web site that contain several answer and php training test
    http://www.php-certifications.com
    It covers all categorys of the Zend PHP 5.3 Certification :

    – PHP Bases
    – Streams and Network Programming
    – Databases and SQL
    – XML and Web Services
    – Object Oriented Programming
    – Security
    – Strings and Patterns
    – Web Features
    – PHP 4/5 Differences
    – Functions
    – Arrays
    – Design and Theory

Leave a Reply

Your email address will not be published. Required fields are marked *

*