Replacing Thumbnail Dimensions

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

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Replacing Thumbnail Dimensions

Post by psurrena »

Hello,

I'm replacing the 525x272 of the following url with 236x190.
http://www.website.com/wp-content/uploa ... 25x272.jpg

I'm using the following pattern but how should I modify it so it works when there are only two numbers or maybe four?

Code: Select all

preg_replace('/[0-9][0-9][0-9]x[0-9][0-9][0-9]/m', "236x190", $image['full'])
Thanks!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Replacing Thumbnail Dimensions

Post by pickle »

Your URL only has 2 numbers - why would there be 4?

Also - if you know the string your replacing, it'd be better to use str_replace()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Replacing Thumbnail Dimensions

Post by twinedev »

I if understand correctly, by having two or 4 digits, you mean the following:
  1. the-image-1024x768.jpg
  2. the-image-131x99.jpg
I believe the following is what you would be looking for:

Code: Select all

preg_replace('/-[0-9]+x[0-9]+\./', '-236x190.', $image['full'])
I included the preceding hyphen and the following period to help limit the matching to a more specific location (ie. what if someone uploads an file whose name already has dimensions, say my-pic-22x33-size-525x272.jpg )

-Greg
Post Reply