www.webdeveloper.com
Results 1 to 5 of 5

Thread: define variable from string segment

  1. #1
    Join Date
    Jul 2010
    Location
    Birmingham, UK
    Posts
    67

    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);
    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.

  2. #2
    Join Date
    Jul 2007
    Location
    Québec
    Posts
    61
    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
    Location
    Ankh-Morpork
    Posts
    19,384
    You could use use preg_match_callback() and apply the logic in the callback function.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    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
    Location
    Birmingham, UK
    Posts
    67

    resolved

    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);
    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