SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick regex question

    Hi

    I need to replace the width and height tags in an iframe HTML string. So for example, the input string will be:

    <iframe width="380" height="230" src="http://www.youtube.com/embed/1T4XMNN4bNM" frameborder="0" allowfullscreen></iframe>

    I would like the output string to be

    <iframe width="400" height="250" src="http://www.youtube.com/embed/1T4XMNN4bNM" frameborder="0" allowfullscreen></iframe>

    I.e. changing the width and height variables.

    Is that easy to do?

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try with str_replace():
    Code:
    $str = '<iframe width="380" height="230" src="http://www.youtube.com/embed/1T4XMNN4bNM" frameborder="0" allowfullscreen></iframe>';
    $str = str_replace(array('width="380"', 'height="230"'), array('width="400"', 'height="250"'), $str);
    echo $str;
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are the starting and ending dimensions always going to be the same then?

    Is this text coming from files, or are they strings in a db?

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    64 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by MarPlo View Post
    Hi,
    Try with str_replace():
    Code:
    $str = '<iframe width="380" height="230" src="http://www.youtube.com/embed/1T4XMNN4bNM" frameborder="0" allowfullscreen></iframe>';
    $str = str_replace(array('width="380"', 'height="230"'), array('width="400"', 'height="250"'), $str);
    echo $str;
    Bah, ugly.

    Code:
    $width = 400;
    $height = 200;
    echo '<iframe width="' . $width . '" height="' . $height . '"  src="http://www.youtube.com/embed/1T4XMNN4bNM" frameborder="0"  allowfullscreen></iframe>';

  5. #5
    SitePoint Member bit101010's Avatar
    Join Date
    Jan 2013
    Posts
    5
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Using regex is kind of pointless, putting more load on the CPU than you need to. It will only be noticeable if you are generating thousands of links, but still. The best solution here would be K. Wolfe's assuming that the embed will remain constant. Also, I just amended the code so it will work with different videos:
    Code:
    $width = 400;
    $height = 200;
    $url = 'http://www.youtube.com/embed/1T4XMNN4bNM';
    echo '<iframe width="' . $width . '" height="' . $height . '"  src="' . $url . '" frameborder="0"  allowfullscreen></iframe>';
    or

    Code:
    $width = 400;
    $height = 200;
    $videoIdentifier = '1T4XMNN4bNM';
    echo '<iframe width="' . $width . '" height="' . $height . '"  src="http://www.youtube.com/embed/' . $url . '" frameborder="0"  allowfullscreen></iframe>';
    Really depends on the situation. Will you be dynamically generating the embed code?

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    If you want a flexible regex, this will do:
    Code:
    $html = '<iframe width=\'380\' height="230" src="http://www.youtube.com/embed/1T4XMNN4bNM" frameborder="0" allowfullscreen></iframe>';
    
    $width = 400;
    $height = 250;
    
    $html = preg_replace('#(\swidth\s*=\s*["\']{0,1})\d+(["\']{0,1})#i', '${1}' .$width. '${2}', $html, 1);
    $html = preg_replace('#(\sheight\s*=\s*["\']{0,1})\d+(["\']{0,1})#i', '${1}' .$height. '${2}', $html, 1);
    This will work even if there is some white space around attributes, which is allowed in html, also it will work for single quotes, double quotes or no quotes at all around the numbers and of course it will work for any numbers. Don't worry about it being slow, regex is not that slow - the above conversion takes 0.00007 seconds!


Bookmarks

Posting Permissions

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