ASP, vbscript - regex replace

I want to replace some text in the following code that users can insert in a CMS system:

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/W8_Kfjo3VjU&hl=nl_NL&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/W8_Kfjo3VjU&hl=nl_NL&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>

As you can see this is from Youtube to embed a video.

I want to replace these strings in the code:

width="480"
height="385"

These strings can be variable. For instance someone can just insert: width="2480"

I want to avoid people from doing that.

Those 2 stings need to be replaced by:

width="320"
height="200"

Can anyone give some code to do that ?

Also any idea's how to do a basic validation that the code people insert is indeed to embed a Youtube video ?
Dutch_guyAsked:
Who is Participating?
 
jostranderCommented:
Maybe something like this:
oldText="<object width=""480"" height=""385""><param name=""movie"" value=""http://www.youtube.com/v/W8_Kfjo3VjU&hl=nl_NL&fs=1&""></param><param name=""allowFullScreen"" value=""true""></param><param name=""allowscriptaccess"" value=""always""></param><embed src=""http://www.youtube.com/v/W8_Kfjo3VjU&hl=nl_NL&fs=1&"" type=""application/x-shockwave-flash"" allowscriptaccess=""always"" allowfullscreen=""true"" width=""480"" height=""385""></embed></object>"

msgbox oldText

newText=FixDimensions(oldText)

msgbox newText




Function FixDimensions(myText)
	
	'Set objRegEx = CreateObject("VBScript.RegExp")
	Set objRegEx = New RegExp

	objRegEx.Global=True
	'objRegEx.MultiLine = True 
	objRegEx.IgnoreCase=True
	
	
	objRegEx.Pattern = "width=""+[0-9]+"""
	Set colMatches_w = objRegEx.Execute(myText)  
	If colMatches_w.count > 0 then myText=objRegEx.Replace(myText,"width=""320""")
	
	objRegEx.Pattern = "height=""+[0-9]+"""
	Set colMatches_h = objRegEx.Execute(myText)  
	If colMatches_h.count > 0 then myText=objRegEx.Replace(myText,"height=""200""")

	FixDimensions=myText

End Function

Open in new window

0
 
Dutch_guyAuthor Commented:
Works great! Thanks alot !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.