define variable from string segment
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:
$find_width = "/width=\"\d+\"/i";
$max_width = "width=\"400\"";
$replace_width = preg_replace($find_width, $max_width, $string);
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.
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().
You could use use preg_match_callback() and apply the logic in the callback function.
Or parse the HTML with DOMDocument. That's the easy way.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
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.
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.
list($width_left, $width_right) = explode("width=\"", $string);
list($value_width, $width_right) = explode("\"", $width_right);
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread