<?
$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);
?>preg_replace -- Perform a regular expression search and replace
Description
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])
Searches subject for matches to pattern and replaces them with replacement. If limit is specified, then only limit matches will be replaced; if limit is omitted or is -1, then all matches are replaced. Replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. 注: When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \\1 notation for your backreference. \\11, for example, would confuse preg_replace() since it does not know whether you want the \\1 backreference followed by a literal 1, or the \\11 backreference followed by nothing. In this case the solution is to use \${1}1. This creates an isolated $1 backreference, leaving the 1 as a literal.
解决方案 »
- 装了个eclipse,如何让它具备调试功能?
- linux系统,Apach的WEB平台,PHPINFO显示空白,为什么?
- 请教php发信功能
- 腾讯首页文章标题一行显示一个或多个怎么做的?
- 干了5年web开发,最近找工作不顺利,ubi面试机会都不给,似乎是闲我跳多了-----你跳了几跳(散分)?
- 关于$_SERVER['SERVER_NAME']问题
- linux中,如何用php把一个域名绑定到某一个目录?
- 招聘 PHP 开发工程师(全职)
- 请问哪儿能下载模块 ming?
- 有冇人识用php来写类似资源管理器的树状目录结构
- 如何判断能否访问网络上的指定网页,也就是看看该网页存不存在。
- 递归问题,高手请进
$s = "English-Chinese Dictionary. English: Books for Learning Chinese available at Amazon.com
Click here to learn Chinese Sayings online Learn Chinese the fun way by taking the Chinese
Quiz online.
";preg_match_all("/.+Chinese/U",$s,$regs);
$p = $regs[0];foreach($p as $k=>$v) {
$p[$k] = "/$v/";
$r[$k] = $v."_".($k+1);
}
echo preg_replace($p,$r,$s);
?>
输出
English-Chinese_1 Dictionary. English: Books for Learning Chinese_2 available at Amazon.com
Click here to learn Chinese_3 Sayings online Learn Chinese_4 the fun way by taking the Chinese_5
Quiz online.
如果源字符串是
English-Chinese Dictionary. English: Books for Learning Chinese available at Amazon.com Click here to learn Chinese Sayings online Learn Chinese the fun way by taking the Chinese Quiz online ChineseChinese.
用你的方法替换后为
English-Chinese_1 Dictionary. English: Books for Learning Chinese_2 available at Amazon.com Click here to learn Chinese_3 Sayings online Learn Chinese_4 the fun way by taking the Chinese_5 Quiz online Chinese_6Chinese.
而我想要的是:
English-Chinese_1 Dictionary. English: Books for Learning Chinese_2 available at Amazon.com Click here to learn Chinese_3 Sayings online Learn Chinese_4 the fun way by taking the Chinese_5 Quiz online Chinese_6Chinese_7. 是最后一个单词的区别~
你再看看这个问题,谢了!
ereg("Chinese",$str,$regs)将每次匹配最后一个Chinese
<?php
$ss=
"English-Chinese Dictionary. English: Books for Learning Chinese available at Amazon.com
Click here to learn Chinese Sayings online Learn Chinese the fun way
by taking the Chinese Quiz online. ChineseChinese";
echo $ss."<br><br>";$arr=explode("Chinese",$ss);
if (count($arr)==1) $ss=$arr[0];
else {
$ss="";
for ($i=0;$i<(count($arr)-1);$i++) {
$ss.=$arr[$i]."Chinese_".($i+1);
}
$ss.=$arr[count($arr)-1];
}echo $ss."<br>";?>