[已解决]请教一个正则替换视频地址的问题
本帖最后由 lpxx 于 2013-8-21 22:52 编辑网站以前使用eWebEditor2.8的编辑器,插入部分优酷视频,插入代码如下:<P style="TEXT-ALIGN: center">
<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0 height=400 width=480 classid=clsid:d27cdb6e-ae6d-11cf-96b8-444553540000><PARAM NAME="_cx" VALUE="12700"><PARAM NAME="_cy" VALUE="10583"><PARAM NAME="FlashVars" VALUE=""><PARAM NAME="Movie" VALUE="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf"><PARAM NAME="Src" VALUE="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf"><PARAM NAME="WMode" VALUE="Window"><PARAM NAME="Play" VALUE="-1"><PARAM NAME="Loop" VALUE="-1"><PARAM NAME="Quality" VALUE="High"><PARAM NAME="SAlign" VALUE=""><PARAM NAME="Menu" VALUE="-1"><PARAM NAME="Base" VALUE=""><PARAM NAME="AllowScriptAccess" VALUE=""><PARAM NAME="Scale" VALUE="ShowAll"><PARAM NAME="DeviceFont" VALUE="0"><PARAM NAME="EmbedMovie" VALUE="0"><PARAM NAME="BGColor" VALUE=""><PARAM NAME="SWRemote" VALUE=""><PARAM NAME="MovieData" VALUE=""><PARAM NAME="SeamlessTabbing" VALUE="1"><PARAM NAME="Profile" VALUE="0"><PARAM NAME="ProfileAddress" VALUE=""><PARAM NAME="ProfilePort" VALUE="0"><PARAM NAME="AllowNetworking" VALUE="all"><PARAM NAME="AllowFullScreen" VALUE="false"><PARAM NAME="AllowFullScreenInteractive" VALUE=""><PARAM NAME="IsDependent" VALUE="0">
<embed height="400" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf" type="application/x-shockwave-flash" width="480"></embed>
</OBJECT></P>
现在由于浏览器兼容问题,需要把以上代码替换为<p style="text-align: center">
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="400" width="480">
<param name="quality" value="high" />
<param name="movie" value="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf" />
<embed height="400" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf" type="application/x-shockwave-flash" width="480"></embed>
</object>
</p>
才能正常显示。
测试了几遍,我不能一次替换成功,请知道朋友的帮忙看看,谢谢。
如果测试通过,打算批量替换可以节约我很多时间和很多的重复的手工劳动。 回复 1# lpxx
如果网页里只有一个 <p style..> ...</p> 应该能直接替换吧?
(?is)<p style.+?</p>
如果有多个...加个 object应该也能识别了吧?
(?is)<p style.+?<object.+?</object></p>
供参考. 回复 2# user3000
感谢关注,是这样的,数据库里这样的视频很多,需要全部替换。可能还有介绍,图片等等包含在页面里,我需要全部替换成下面标准格式,我的正则如下。Local $Test = StringRegExpReplace($str, '\<OBJECT([\s\S.]*?\>(.*?)\<).*?\<\/OBJECT\>', '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="400" width="480"><param name="quality" value="high" /><param name="movie" value="$2" /><embed height="400" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="$2" type="application/x-shockwave-flash" width="480"></embed></object>', 0)
本帖最后由 user3000 于 2013-8-21 13:10 编辑
回复 3# lpxx
客气了.
按我的经验,使用 StringRegExpReplace ,如果不是调整原来一些内容的位置,不要用捕获组 "()".
捕获组 "\0 或 $0对应整个字符串
\1 $1 对应第一个()捕获组....
,,,,
可以这样使用捕获组: StringRegExpReplace($str, '(.+)(<OBJECT.+?</OBJECT>)(.+)', '\1' & $replace & '\3')
但我觉得这里使用捕获组有点多余
< / 等不用转义.
最好放一整个网页源码上来,供他人测试吧.
先这样,祝你成功. 我上班去咯!
还有处理网页源码, 最好加上不区别大小写的标志 (?i). 很多时候,看到的跟真正的字符是不一样的(大小写). 本帖最后由 afan 于 2013-8-21 16:36 编辑
捕获组,引用替换到需要的位置就行了,是个细活…Local $Str = ''
$Str &= '<P style="TEXT-ALIGN: center">' & @CRLF
$Str &= '<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0 height=400 width=480 classid=clsid:d27cdb6e-ae6d-11cf-96b8-444553540000><PARAM NAME="_cx" VALUE="12700"><PARAM NAME="_cy" VALUE="10583"><PARAM NAME="FlashVars" VALUE=""><PARAM NAME="Movie" VALUE="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf"><PARAM NAME="Src" VALUE="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf"><PARAM NAME="WMode" VALUE="Window"><PARAM NAME="Play" VALUE="-1"><PARAM NAME="Loop" VALUE="-1"><PARAM NAME="Quality" VALUE="High"><PARAM NAME="SAlign" VALUE=""><PARAM NAME="Menu" VALUE="-1"><PARAM NAME="Base" VALUE=""><PARAM NAME="AllowScriptAccess" VALUE=""><PARAM NAME="Scale" VALUE="ShowAll"><PARAM NAME="DeviceFont" VALUE="0"><PARAM NAME="EmbedMovie" VALUE="0"><PARAM NAME="BGColor" VALUE=""><PARAM NAME="SWRemote" VALUE=""><PARAM NAME="MovieData" VALUE=""><PARAM NAME="SeamlessTabbing" VALUE="1"><PARAM NAME="Profile" VALUE="0"><PARAM NAME="ProfileAddr'
$Str &= 'ess" VALUE=""><PARAM NAME="ProfilePort" VALUE="0"><PARAM NAME="AllowNetworking" VALUE="all"><PARAM NAME="AllowFullScreen" VALUE="false"><PARAM NAME="AllowFullScreenInteractive" VALUE=""><PARAM NAME="IsDependent" VALUE="0"> ' & @CRLF
$Str &= ' <embed height="400" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf" type="application/x-shockwave-flash" width="480"></embed> ' & @CRLF
$Str &= '</OBJECT></P>'
MsgBox(0, '原字符串', $Str)
Local $sRE = StringRegExpReplace($str, '(?si)(\h+\S+se\=)(\S+)(.+?\=)(\d+)(.+?\=)(\d+)(.+?\=)([^\>]+).+?>(<[^<>]+)(http:.+?")>.+?(<[^<>]+?"Quality"[^<>]+)>.+?(<e.+)</P>\s*$', '$7"$8"$1"$2"$3"$4"$5"$6">' & @CRLF & '$11 />' & @CRLF & '$9$10 />' & @CRLF & '$12' & @CRLF & '</P>')
MsgBox(0, '替换结果', $sRE)果真细活,开始忘了个>号… 本帖最后由 lpxx 于 2013-8-21 15:54 编辑
谢谢A版,晚上我试试看
我的正则是Local $Test = StringRegExpReplace($str, '\<OBJECT([\s\S.]*?\>(.*?)\<).*?\<\/OBJECT\>', '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="400" width="480"><param name="quality" value="high" /><param name="movie" value="$2" /><embed height="400" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="$2" type="application/x-shockwave-flash" width="480"></embed></object>', 0)
总觉得不对,看了你的才发现,我的彻底错了。
Local $sText = _
'<P style="TEXT-ALIGN: center">' & @CRLF & _
'<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0 height=400 width=480 classid=clsid:d27cdb6e-ae6d-11cf-96b8-444553540000><PARAM NAME="_cx" VALUE="12700"><PARAM NAME="_cy" VALUE="10583"><PARAM NAME="FlashVars" VALUE=""><PARAM NAME="Movie" VALUE="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf"><PARAM NAME="Src" VALUE="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf"><PARAM NAME="WMode" VALUE="Window"><PARAM NAME="Play" VALUE="-1"><PARAM NAME="Loop" VALUE="-1"><PARAM NAME="Quality" VALUE="High"><PARAM NAME="SAlign" VALUE=""><PARAM NAME="Menu" VALUE="-1"><PARAM NAME="Base" VALUE=""><PARAM NAME="AllowScriptAccess" VALUE=""><PARAM NAME="Scale" VALUE="ShowAll"><PARAM NAME="DeviceFont" VALUE="0"><PARAM NAME="EmbedMovie" VALUE="0"><PARAM NAME="BGColor" VALUE=""><PARAM NAME="SWRemote" VALUE=""><PARAM NAME="MovieData" VALUE=""><PARAM NAME="SeamlessTabbing" VALUE="1"><PARAM NAME="Profile" VALUE="0"><PARAM NAME="ProfileAddress" VALUE=""><PARAM NAME="ProfilePort" VALUE="0"><PARAM NAME="AllowNetworking" VALUE="all"><PARAM NAME="AllowFullScreen" VALUE="false"><PARAM NAME="AllowFullScreenInteractive" VALUE=""><PARAM NAME="IsDependent" VALUE="0"> ' & @CRLF & _
' <embed height="400" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="http://player.youku.com/player.php/sid/XMjc5NjA3NzA4/v.swf" type="application/x-shockwave-flash" width="480"></embed> ' & @CRLF & _
'</OBJECT></P>'
;~ 沒有引號不知會不會造成影響
;~ $sText = StringRegExpReplace($sText, '(?i)( (?:codeBase|classid)=)([^\s>]+)', '$1"$2"')
$sText = StringRegExpReplace($sText, '(?i)<param name="(?!Movie"|Quality")[^>]+>', "")
ConsoleWrite($sText & @CRLF)
Exit
页:
[1]