Zend PHP5 认证考试研究之一:Strings 和 Regular Expressions
在这个帖子中,我列出你要刷新你的记忆的关于PHP5中String和Regular Expressions功能的一些方面。String 和 Regx 对于任何网络开发语言来说都是非常重要的,因此,在你接受考试之前你必须对它们有一个很好的领会。在这里列出的一些函数/功能并不保证它们会出现在考试中,它们只是我认为你需要知道的东西。当然,这只是在php.net上有关功能/函数的一小片断;我鼓励你经常访问php official manual网站!
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.
感谢您阅读本文章。您现在可以 Leave A Comment (0)
日志信息
本日志发布于 1月 4, 2008 8:40 下午 发布在 php 标记为:技术,网站开发,Zend认证,php你可以跟进任何对此文章的回复通过 Comments Feed. 你可以 留一个评论 ,或 一个 Trackback .
前一日志 通过Zend 的PHP5的认证考试 »
下一日志 Zend PHP5 认证考试研究之二:Streams 和 Network »
- Review, php|architect’s Guide to Programming with Zend Framework
- Use Writer, the online Darkroom version!
- Learn to Design Web Themes and Templates with New Wiki
- 国谷哥发布: 四川震区急需 260 万顶帐篷
- WordPress problem: Url encoding on Tag’s slug.
- Curl better than simplexml_load_file when web screen scrapping
- How to use SimpleXML parsing XML data with namespace and CDATA
- 关于生活的哲学
- Zend PHP5 认证考试研究之12: 附三
- Zend PHP5 认证考试研究之11: 附二
english











