对不起写错了例如:
<IMG src="path/filename.ext" width=100 height=200>如何转换成<img src=path/filename.ext" width="100" height="200" alt=""/>
-----<IMG src="path/filename.ext" width=100 height=200 alt="LOGO">如何转换成<img src=path/filename.ext" width="100" height="200" alt="LOGO"/>
<IMG src="path/filename.ext" width=100 height=200>如何转换成<img src=path/filename.ext" width="100" height="200" alt=""/>
-----<IMG src="path/filename.ext" width=100 height=200 alt="LOGO">如何转换成<img src=path/filename.ext" width="100" height="200" alt="LOGO"/>
src少写了个引号吧?帮你顶。
<IMG src="path/filename.ext" width=100 height=200 alt="LOGO">
HTML;
echo preg_replace("/<img (.+?)width=(.+?)height=(.+?) (.+?)>/isU", "<img \\1width=\"\\2\"height=\"\\3\" \\4/>", $str);转换结果:
<img src="path/filename.ext" width="100 "height="200" alt="LOGO"/>
http://community.csdn.net/Expert/topic/3186/3186874.xml?temp=.3436396
里面的回答还不错,可以将各个属性都得到
如果XHTML化,只要再加上几个判断就行
$s = <<< TEXT
<IMG src="path/filename.ext" width=100 height=200 alt="LOGO">
TEXT;function foo($v) {
$v = preg_replace("/[\"']/","",stripslashes($v));
$p = split("=",$v);
if(count($p) == 2)
return "$p[0]=\"$p[1]\"";
return $v;
}$s = preg_replace("/(\b[^ >]+)/e","foo('$1')",$s);
echo preg_replace("/>/","/>",$s);
?>至于如何添加没有的属性?如何区分目标?...
就由你自己解决了