模式匹配替换字符的问题

问题如下:

将字符串中[item]1[/item]替换为一个数组arrayA(1)的内容

[item]2[/item]替换为arrayA(2)
类推。

可以通过模式匹配替换实现吗?
---------------------------------------------------------------

1   
2$ar[1] = "1234";   
3$v = preg_replace("/\\[item\\](\d)\\[\/item\\]/","\$ar[\\\1]","[item]1[/item]");   
4eval("\$k=\"$v\";");   
5echo $k;   

---------------------------------------------------------------

好像还可以这样。
你建立一个数组,
索引是“源字符串”,值则是“目标字符串”
比如这样,
function conversions($arg)
{
$trans = array(":)" => "

1<img height='\"19\"' src='\"../img/emoticons/regular_smile.gif\"' width='\"19\"'/>

",
":(" => "

1<img height='\"19\"' src='\"../img/emoticons/sad_smile.gif\"' width='\"19\"'/>

",
":'(" => "

1<img height='\"19\"' src='\"../img/emoticons/cry_smile.gif\"' width='\"19\"'/>

",
":@" => "

1<img height='\"19\"' src='\"../img/emoticons/angry_smile.gif\"' width='\"19\"'/>

");//中间省略了很多
$result = strtr($arg, $trans);
return $result;
}
(注:不适用于动态生成的数组 -clapton)

Published At
Categories with Web编程
Tagged with
comments powered by Disqus