regex image tag

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jphamac
Forum Newbie
Posts: 6
Joined: Fri Sep 23, 2005 11:56 pm

regex image tag

Post by jphamac »

I want to post images on my blog but don not want to break the table. I need a code that searches through my string to find image tags, then finds the size of that image. Then it checks if the image is too big and resize it down. I don't need the image to be physically resized but instead just have the html width and height declare it.

Can someone pleaes help. I can do the size conversion to keep the image proportional. i just need the regex that searches for image tags and replace it.

basically, i want to convert:
<img src="http://www.myimage.jpg">

to something like:
<img src-"ttp://www.myimage.jpg" width="500" height="400">


Would it be something like this?

$anchor = preg_replace('/\<img src="([^\]]+)\]([^\[]+)\[\/">\]/i', "<img src=\"$1\">$2 height=\"$h\" width=\"$w\"\">", $string);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your pattern seems way off.. look over some of the recent threads in this (regex) board for ideas of how to capture the data you need to..
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

I'd physically resize it myself, then turn it into a link that gave a popup of the fullsized image.

Anyway... Let's see... Assuming all images are in the exact format <img src="value">:

Code: Select all

$txt = preg_replace('/(<img src=".+?")>/','$1 width="500" height="400">',$txt);
Now to take into account all variations of the image tag...

Code: Select all

$txt = preg_replace("#(<img[^>]+?)/?>#is","$1 width='500' height='400' />",$txt);
I hope I got that right... >.>
jphamac
Forum Newbie
Posts: 6
Joined: Fri Sep 23, 2005 11:56 pm

Post by jphamac »

Thanks you so much for helping me out! That's exactly what i wanted.

I originally wanted to get the image size and size it down proportionally but i don't know how to extract just the url of the image so that i can do a imagesize() on it. How do you extract just the image's location?

Your code is perfect but it doesn't work if the tag has border=""
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Your code is perfect but it doesn't work if the tag has border=""
Are you using the first or second one? The first one certainly won't catch it, but the second one should.
How do you extract just the image's location?

Code: Select all

preg_match("#<img.+?src=['\"]([^'\"]+?)['\"][^>]+?>#is",$text,$matches);
//$matches will be an array that holds the info you need.
Should work.. untested.
jphamac
Forum Newbie
Posts: 6
Joined: Fri Sep 23, 2005 11:56 pm

Post by jphamac »

Scara, thanks you so much. you've been so helpful! :D
Post Reply