PCRE 函数
目录
- preg_filter — 执行一个正则表达式搜索和替换
- preg_grep — 返回匹配模式的数组条目
- preg_last_error_msg — Returns the error message of the last PCRE regex execution
- preg_last_error — 返回最后一个PCRE正则执行产生的错误代码
- preg_match_all — 执行一个全局正则表达式匹配
- preg_match — 执行匹配正则表达式
- preg_quote — 转义正则表达式字符
- preg_replace_callback_array — Perform a regular expression search and replace using callbacks
- preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调进行替换
- preg_replace — 执行一个正则表达式的搜索和替换
- preg_split — 通过一个正则表达式分隔字符串

User Contributed Notes 3 notes
steve at stevedix dot de ¶
17 years ago
Something to bear in mind is that regex is actually a declarative programming language like prolog : your regex is a set of rules which the regex interpreter tries to match against a string. During this matching, the interpreter will assume certain things, and continue assuming them until it comes up against a failure to match, which then causes it to backtrack. Regex assumes "greedy matching" unless explicitly told not to, which can cause a lot of backtracking. A general rule of thumb is that the more backtracking, the slower the matching process.
It is therefore vital, if you are trying to optimise your program to run quickly (and if you can't do without regex), to optimise your regexes to match quickly.
I recommend the use of a tool such as "The Regex Coach" to debug your regex strings.
http://weitz.de/files/regex-coach.exe (Windows installer) http://weitz.de/files/regex-coach.tgz (Linux tar archive)
stronk7 at moodle dot org ¶
14 years ago
One comment about 5.2.x and the pcre.backtrack_limit:
Note that this setting wasn't present under previous PHP releases and the behaviour (or limit) under those releases was, in practise, higher so all these PCRE functions were able to "capture" longer strings.
With the arrival of the setting, defaulting to 100000 (less than 100K), you won't be able to match/capture strings over that size using, for example "ungreedy" modifiers.
So, in a lot of situations, you'll need to raise that (very small IMO) limit.
The worst part is that PHP simply won't match/capture those strings over pcre.backtrack_limit and will it be 100% silent about that (I think that throwing some NOTICE/WARNING if raised could help a lot to developers).
There is a lot of people suffering this changed behaviour from I've read on forums, bugs and so on).
Hope this note helps, ciao :-)
Svoop ¶
13 years ago
I have written a short introduction and a colorful cheat sheet for Perl Compatible Regular Expressions (PCRE):
http://www.bitcetera.com/en/techblog/2008/04/01/regex-in-a-nutshell/