Results 1 to 5 of 5

Thread: define variable from string segment

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Birmingham, UK

    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:

    $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.

  2. #2
    Join Date
    Jul 2007
    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().

  3. #3
    Join Date
    Aug 2004
    You could use use preg_match_callback() and apply the logic in the callback function.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Join Date
    Nov 2002
    Baltimore, Maryland
    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

  5. #5
    Join Date
    Jul 2010
    Birmingham, UK


    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.

    list($width_left, $width_right) = explode("width=\"", $string);
    list($value_width, $width_right) = explode("\"", $width_right);
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles