当前位置: 代码迷 >> PHP >> 替换结构再判断解决办法
  详细解决方案

替换结构再判断解决办法

热度:130   发布时间:2012-06-01 16:46:36.0
替换结构再判断
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"><param name="movie" value="/images/1/17/ESIL_xiaoshengqi2.swf"><param name="bgcolor" value="#FFFFFF"><param name="scale" value="exactfit">
<embed src="/images/1/17/ESIL_xiaoshengqi2.swf" width="550" height="400" bgcolor="#FFFFFF" scale="exactfit" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed> </object>

改为:

<img src="../Documents/medies/ESIL_xiaoshengqi1.gif" alt="Image:ESIL_xiaoshengqi1.gif" width="550" height=" 400" longdesc="/index.php/Image:ESIL_xiaoshengqi2.gif" />

width和height的值是变动的,如果width < 805, 那么width和height的值都不需要改变。
如果width > 805 
那么height = height * 805 /width ; 
  width= 805

这个正则加判断怎么写啊?最近用到好多正则,怎么样才能把正则学好呢?


------解决方案--------------------
请说明你的规则

../Documents/medies/ESIL_xiaoshengqi1.gif
这个乍来的


Image:ESIL_xiaoshengqi1.gif 这个呢?



width="550" height=" 400" 这个呢???上面有两个,需要哪个?



------解决方案--------------------
参考一下:
PHP code
$s=<<<html
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"><param name="movie" value="/images/1/17/ESIL_xiaoshengqi2.swf"><param name="bgcolor" value="#FFFFFF"><param name="scale" value="exactfit">
<embed src="/images/1/17/ESIL_xiaoshengqi2.swf" width="1000" height="400" bgcolor="#FFFFFF" scale="exactfit" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed> </object>
html;

$r= preg_replace('#.+<embed\s*src="(.*?)/([^"]+)"\s*width="(\d+)"\s*height="(\d+)"[^>]*>.+#s','<img src="../Documents/medies/$2" alt="Image:$2" width="$3" height="$4" longdesc="/index.php/Image:$2" />',$s);

preg_match('#width="(\d+)"\s*height="(\d+)"#i',$r,$m);
if($m[1]>805){ 
         $h=($m[2]*805)/$m[1] ; 
         $r=preg_replace('/(width=)"(\d+)"(\s*height=)"(\d+)"/s','$1"$2"$3"'.$h.'"',$r);
}
echo $r;

------解决方案--------------------
PHP code
$s =<<< TXT
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"><param name="movie" value="/images/1/17/ESIL_xiaoshengqi2.swf"><param name="bgcolor" value="#FFFFFF"><param name="scale" value="exactfit">
<embed src="/images/1/17/ESIL_xiaoshengqi2.swf" width="1550" height="400" bgcolor="#FFFFFF" scale="exactfit" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed> </object>
TXT;
$r= preg_replace_callback('#.+<embed\s*src="(.*?)/([^"]+)"\s*width="(\d+)"\s*height="(\d+)"[^>]*>.+#s', 'back', $s);

function back($r) {
  if($r[3] > 805) {
    $r[4] = round($r[4]*805/$r[3]);
    $r[3] = 805;
  }
  return <<< TXT
<img src="../Documents/medies/$r[2]" alt="Image:$r[2]" width="$r[3]" height="$r[4]" longdesc="/index.php/Image:$r[2]" />
TXT;
}
echo $r; 
  相关解决方案