|
|
 |
preg_replace (PHP 3>= 3.0.9, PHP 4 , PHP 5) preg_replace -- 执行正则表达式的搜索和替换 说明mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])
在
subject 中搜索
pattern 模式的匹配项并替换为
replacement。如果指定了
limit,则仅替换
limit 个匹配,如果省略
limit 或者其值为 -1,则所有的匹配项都会被替换。
replacement 可以包含
\\n
形式或(自 PHP 4.0.4 起)$n
形式的逆向引用,首选使用后者。每个此种引用将被替换为与第
n
个被捕获的括号内的子模式所匹配的文本。n
可以从 0 到 99,其中
\\0 或 $0
指的是被整个模式所匹配的文本。对左圆括号从左到右计数(从 1 开始)以取得子模式的数目。
对替换模式在一个逆向引用后面紧接着一个数字时(即:紧接在一个匹配的模式后面的数字),不能使用熟悉的
\\1 符号来表示逆向引用。举例说
\\11,将会使
preg_replace() 搞不清楚是想要一个
\\1 的逆向引用后面跟着一个数字
1 还是一个
\\11 的逆向引用。本例中的解决方法是使用
\${1}1。这会形成一个隔离的
$1 逆向引用,而使另一个
1 只是单纯的文字。
例子 1. 逆向引用后面紧接着数字的用法 |
<?php
$string = "April 15, 2003";
$pattern = "/(\w+) (\d+), (\d+)/i";
$replacement = "\${1}1,\$3";
print preg_replace($pattern, $replacement, $string);
/* Output
======
April1,2003
*/
?>
|
|
如果搜索到匹配项,则会返回被替换后的
subject,否则返回原来不变的
subject。
preg_replace() 的每个参数(除了
limit)都可以是一个数组。如果
pattern 和
replacement 都是数组,将以其键名在数组中出现的顺序来进行处理。这不一定和索引的数字顺序相同。如果使用索引来标识哪个
pattern 将被哪个
replacement 来替换,应该在调用
preg_replace() 之前用
ksort() 对数组进行排序。
例子 2. 在 preg_replace() 中使用索引数组 |
<?php
$string = "The quick brown fox jumped over the lazy dog.";
$patterns[0] = "/quick/";
$patterns[1] = "/brown/";
$patterns[2] = "/fox/";
$replacements[2] = "bear";
$replacements[1] = "black";
$replacements[0] = "slow";
print preg_replace($patterns, $replacements, $string);
/* Output
======
The bear black slow jumped over the lazy dog.
*/
/* By ksorting patterns and replacements,
we should get what we wanted. */
ksort($patterns);
ksort($replacements);
print preg_replace($patterns, $replacements, $string);
/* Output
======
The slow black bear jumped over the lazy dog.
*/
?>
|
|
如果
subject 是个数组,则会对
subject 中的每个项目执行搜索和替换,并返回一个数组。
如果
pattern 和
replacement 都是数组,则
preg_replace() 会依次从中分别取出值来对
subject 进行搜索和替换。如果
replacement 中的值比
pattern 中的少,则用空字符串作为余下的替换值。如果
pattern 是数组而
replacement 是字符串,则对
pattern 中的每个值都用此字符串作为替换值。反过来则没有意义了。
/e 修正符使
preg_replace() 将
replacement 参数当作
PHP 代码(在适当的逆向引用替换完之后)。提示:要确保
replacement 构成一个合法的
PHP 代码字符串,否则
PHP 会在报告在包含
preg_replace() 的行中出现语法解析错误。
例子 3. 替换数个值 |
<?php
$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/",
"/^\s*{(\w+)}\s*=/");
$replace = array ("\\3/\\4/\\1\\2", "$\\1 =");
print preg_replace ($patterns, $replace, "{startDate} = 1999-5-27");
?>
|
本例将输出:
|
例子 4. 使用 /e 修正符 |
<?php
preg_replace ("/(<\/?)(\w+)([^>]*>)/e",
"'\\1'.strtoupper('\\2').'\\3'",
$html_body);
?>
|
这将使输入字符串中的所有 HTML 标记变成大写。
|
例子 5. 将 HTML 转换成文本 |
<?php
// $document 应包含一个 HTML 文档。
// 本例将去掉 HTML 标记,javascript 代码
// 和空白字符。还会将一些通用的
// HTML 实体转换成相应的文本。
$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
"'<[\/\!]*?[^<>]*?>'si", // 去掉 HTML 标记
"'([\r\n])[\s]+'", // 去掉空白字符
"'&(quot|#34);'i", // 替换 HTML 实体
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"); // 作为 PHP 代码运行
$replace = array ("",
"",
"\\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
$text = preg_replace ($search, $replace, $document);
?>
|
|
注:
limit 参数是 PHP 4.0.1pl2 之后加入的。
参见 preg_match(),preg_match_all()
和 preg_split()。
toxic79_spam at yahoo dot com
11-Feb-2003 09:45
here's another way to link urls in text. unlike the others i see here, this
one doesn't link urls that have already been linked so you won't get <a
href="<a href="http://www.blah.com">http://www.blah.com</a>">link</a>
if your text ($txt) already has links in it.
it's been quickly
tested with uppercase, lowercase, http and ftp, secure urls, ports and
query strings. it won't work if the url has spaces in it, but if you
convert them to %20 it'll be ok.
$txt = preg_replace(
"/(?<!<a
href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a
href=\"\\0\">\\0</a>", $txt );
| |