/    Sign up×
Community /Pin to ProfileBookmark

define variable from string segment

hi all,

i’m having a problem with finding an replacing certain values in strings passed to a message board. basically, i have things set up so users can post videos or images, but i want to limit the dimensions to, say, 400px in width so i don’t have everthing shifting around on the page. i can easily do this with the following string replacement method:

[CODE]
$find_width = “/width=”d+”/i”;
$max_width = “width=”400″”;
$replace_width = preg_replace($find_width, $max_width, $string);
[/CODE]

simple.

what i would like to do, though, is find the original width value and determine if it is over my max value, and if so replace it with the max value. that way smaller images are not scaled up. also i think it would be useful for finding the value of the height and scaling it in proportion to width of the file instead of supplying an arbitary height value and risk stretching the image.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NihilisteOct 01.2010 — You should have a look at preg_match().

This way you could get original width and then make a comparison with your max_width, before doing the preg_replace().
Copy linkTweet thisAlerts:
@NogDogOct 01.2010 — You could use use preg_match_callback() and apply the logic in the callback function.
Copy linkTweet thisAlerts:
@CharlesOct 01.2010 — Or parse the HTML with DOMDocument. That's the easy way.
Copy linkTweet thisAlerts:
@mimewearauthorOct 01.2010 — thanks for the responses, but i got there in the end. my solution was to look for the start of the width code in whatever the user had posted, in this case "width="". from there i exploded the code into two parts, left and right, and exploded the right part of the code again to leave the value of the width.

[CODE]
list($width_left, $width_right) = explode("width="", $string);
list($value_width, $width_right) = explode(""", $width_right);
[/CODE]


once i could determine the value of the object's width, i could compare it to my max allowed width and adjust it if needed. it was actually way easier to do than i had expected.
×

Success!

Help @mimewear spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...