正则表达式应用
今天同事问我一个正则表达式的问题,我试了半天才搞定,记录一下:)
#该php脚本的目的是将<img ...>替换成<img .../>,如果已经是<img .../>了,则不做修改
$string = "<img src=\"http://cn.yimg.com/i/cn/webmsgr/smileys/small/1.gif\" border=\"0\">";
$string1 = "<img src=\"http://cn.yimg.com/i/cn/webmsgr/smileys/small/1.gif\" border=\"0\"/>";
$pattern = "/(<img) (.*)([^\/])(>)/i";
$replacement = "\${1} \${2}\${3}/>";
echo preg_replace($pattern, $replacement, $string);
echo "\n";
echo preg_replace($pattern, $replacement, $string1);
echo "\n";