Replacing width,height values of EMBED, IMG tags


I need to replace embed and img which are submitted by users. as you know, some of users try to add video or image file links into textfield.

I found some of examples of regex function in order to replace img and embed tag's WIDTH and HEIGHT values but there is a big problem.

I need a function that can replace those dimensions with SCALE PROPOTIONS. its like croping an image that is uploaded. for example user wrote this code in textfield :

<img src="" width="1400" height="1000">

and I have specific maximum WIDTH and HEIGHT values :

max_width = 700
max_height 700

is it possible to replace  that tag like (in order to max width-height values):

<img src="" width="700" height="500">

thank you
Who is Participating?
tokyoaheadConnect With a Mentor Commented:
if you do not want to write this yourself you can also go and take pre-made "BBCode" classes that do similar things. You might have to tinker with the code a bit to make it do what you want exactly though....

here is an example class:

if you search online for "bbcode class" you will find much more,
well it is possible. but you will have a problem if the image code is missing any of the elements, switches them around or leaves away the quotes. they can leave the widht and heigt also away and the image will appear in orginal size... maybe much wider than your page. Your code will be quite fat to accommodate all the eventualities.

I would suggest the following: use a special code. If they want to add an image, they have to use
You will then read this and translate it into <img style="width:100%" src="">

why this code? first of all you do not know the proportions of the image. so defining a height and width is not a good idea unless you know what the dimensions are exactly. second, if you use the style, you can use correct HTML (width="100%" is not). third, you can remove all other fancy HTML that might be a security issue in the first place.

You can on top of that allow them to do a
[image: 50]
which will result in a <img style="width:100%" src="">. Of course you can also do a <img style="max_width:700px" src=""> for a 100/missing width, and reduce the width accordingly to make it smaller.

lots of options here.
sorry, typo. the first sentence in the last paragraph should read:

You can on top of that allow them to do a
[image: 50]
which will result in a <img style="width:50%" src="">.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

middleofnowhereAuthor Commented:
I get it. actually you're right about code sentence is given by user. I made a mistake. probably they will not give any size for images. they will just give links. so your code seems appropiate. but I have 2 questions :

1- I use striptags() function in order to filter any unwanted tags. I just give them <b><a><embed><img>. so how can I translate a code like [image:]. actually default percent values can be given by me thats ok. but still I dont know how can I use regex() function to turn [image:] code into <img width...> ? could you show me a function for that?

2- img codes all right with above but video <embed> codes have always width and height values. what would you suggest for that? percent width values is looking cool but is that appropriate for embed codes?

thank you for your answer
You do not need to use regex to do that. You can use strpos, which is much faster then regex (and less brainy, too :-)

You do a strpos for "[image:", then in a substr( of the string so that you cut off the comment before "[image:", then you do a strpos for "]" and you have your [image: .....] completely localized. you repeat for the string until strpos for [image: is false.

each [image you replace with <img ... > etc. you simply explode the string between [image: and ] by spaces  and get an array with the relevan elements.

If you set it up properly, you can use many different tags, not only "image" but also video etc.

for the size of the video I would recommend that you do a 640x480, which is std TV resolution, that should be fine. I am not sure if a code like that would distort the video anyhow. Otherwise you can do a code like [image: 640 480]

middleofnowhereAuthor Commented:
thank you for information. I've never worked with bbcode before. I'll check that.
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.