corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » PHP Regex Geeks?


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author PHP Regex Geeks?
ed
Member

Registered: 10th Sep 03
User status: Offline
25th Sep 09 at 16:28   View User's Profile U2U Member Reply With Quote

Is there anyone on here who is handy with regular experssions and PHP?

I'm using FCKEditor on my website, but it outputs images like this:

<img src="http://ecsid.co.uk/images/picture.jpg" width="100" height="50" />

I'm not too keen on setting the size of the image using HTML as browsers tend to do a crappy job of resizing the image, as well as having to download the image at full res.

What I normally do is use a script which dynamically resizes the image with a touch of mod_rewrite like so:

<img src="http://ecsid.co.uk/images/100x50/picture.jpg" />

Because I can't figure out how to mod FCKEditor to do what I want, does anyone know how I could write a little function to convert the former to the latter?

xxx
AndyKent
Member

Registered: 3rd Sep 05
User status: Offline
25th Sep 09 at 17:29   View User's Profile U2U Member Reply With Quote

So you want a PHP script that takes

<img src="http://ecsid.co.uk/images/picture.jpg" width="100" height="50" />

in and outputs

<img src="http://ecsid.co.uk/images/100x50/picture.jpg" />

right?

Pretty easy. Whenever you come across a set of img tags split the string at the quotation marks. In the string array you just grab items 2, 4 and 6.
4 and 6 will be the width by height, 2 will be the image address. Split the address at the forward slashes and just insert the height at the right place.

Is that what you were after or do you not know PHP at all?
ed
Member

Registered: 10th Sep 03
User status: Offline
25th Sep 09 at 17:50   View User's Profile U2U Member Reply With Quote

I was wanting to use regex rather than splitting it because if for some reason the width and height tags are in different places within the image code the script wouldn't work.
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
25th Sep 09 at 18:11   View User's Profile U2U Member Reply With Quote

Why dont you just manually change it in the source part of fckeditor?
AndyKent
Member

Registered: 3rd Sep 05
User status: Offline
25th Sep 09 at 18:21   View User's Profile U2U Member Reply With Quote

If its autogenerated, it should be in the same place in the same order every time surely?
ed
Member

Registered: 10th Sep 03
User status: Offline
25th Sep 09 at 19:05   View User's Profile U2U Member Reply With Quote

But you have the option to insert HTML manually too, so it might not always be in that order.
AndyKent
Member

Registered: 3rd Sep 05
User status: Offline
25th Sep 09 at 19:07   View User's Profile U2U Member Reply With Quote

Oh I see. I don't know regex at all, so I would just be splitting as said, maybe compare the text strings pulled out before the numbers so you can 'read' which measurement it is even if its in a different order.
Tim
Site Administrator

Avatar

Registered: 21st Apr 00
User status: Offline
25th Sep 09 at 22:40   View Garage View User's Profile U2U Member Reply With Quote

code:

$url = "<img src="http://ecsid.co.uk/images/picture.jpg" width="100" height="50" />";
$pattern = array('/images\//', 'width=\"[0-9]?\"', 'height=\"[0-9]?\"' );
$replace = array('images\/100x50\/', '', '');
echo preg_replace($pattern, $replace, $url);



Don't blame me if that doesn't work as it hasn't been tested!

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Nakamishi headunits NovaGTE Help Zone, Modification and ICE Advice 29 3027
26th Oct 05 at 10:37
by Fear
 
Corsa turbo Vs MR2 Rich_J_Williams General Chat 104 5369
3rd Apr 06 at 19:52
by Steve
 
Fones for sale for u geeks! Munchie Geek Day 0 656
22nd Jun 06 at 11:26
by Munchie
 
geeks do you want a new dell 20" LCD screen? SetH Geek Day 5 814
27th Dec 06 at 14:10
by Jamie Walby
 
PHP Coding - Contact Form "IF" Statements liamC Geek Day 6 177
1st Sep 09 at 13:08
by liamC
 

Corsa Sport » Message Board » Off Day » Geek Day » PHP Regex Geeks? 29 database queries in 0.0141149 seconds