SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Regex Again :(

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Regex Again :(

    Every time I come across regex I fail lol Could someone please let me know what I'm doing wrong this time:

    PHP Code:
    preg_replace('/width=\"[0-9]+\"/'"width=\"450\""$code); 
    I've got a string ($code) width contains a in the form: width = "ddd" where d stands for digit. I'm trying to replace that entire sub-string with width = "450".

    So far it appears that it is not matching my expression, but width = "600" is definately in there.

    Any help would be greatly appreciated! Nick
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The RegExp looks fine, albeit the escaping in the first parameter is unnecessary.

    Can you post the HTML you're passing in $code?

    Alcar...

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Alcar,

    Stopped escaping the quotation marks, still isn't working. Here's the $code:

    Code HTML4Strict:
    <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/l5lZIzGfb9E&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/l5lZIzGfb9E&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

    And here's my replace code:

    PHP Code:
                           preg_replace('/width="[0-9]+"/'"width=\"450\""$code); 
                           
    preg_replace('/height="[0-9]+"/'"height=\"345\""$code); 
    Thanks heaps! Nick
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works fine on my end (PHP 5.2.0) using:

    PHP Code:
    $code '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/l5lZIzGfb9E&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/l5lZIzGfb9E&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';

    $code preg_replace('/width="[0-9]+"/'"width=\"450\""$code);
    $code preg_replace('/height="[0-9]+"/'"height=\"345\""$code);

    // dump formatted text to check correct replacement
    echo str_replace(array('<''>'), array('&lt;''&gt;'), $code); 
    Are you properly setting $code each time you use preg_replace()? As $code isn't passed by reference to preg_replace().

    Alcar...

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm still not working for me. I'm getting the code from a form, and I don't do the whole "real_escape_string" thing till after this piece of code I'm struggling with. So I've tried stripping slashes:

    PHP Code:

    $code 
    stripslashes($code);
    preg_replace('/width="[0-9]+"/'"width=\"450\""$code); 
    preg_replace('/height="[0-9]+"/'"height=\"345\""$code); 
    echo 
    str_replace(array('<''>'), array('&lt;''&gt;'), $code); 
    Here is my output from that echo:

    <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/l5lZIzGfb9E&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/l5lZIzGfb9E&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As I mentioned in my previous post, preg_replace() doesn't pass $code as a reference - instead it returns the replaced value without modifying the contents of $code.

    If you omit the bold parts highlighted in the code below (which you are doing), the result won't be captured in $code.

    $code = preg_replace('/width="[0-9]+"/', "width=\"450\"", $code);
    $code = preg_replace('/height="[0-9]+"/', "height=\"345\"", $code);


    So you MUST include the $code = parts.

    Alcar...

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha C&*P, I'm so sorry, that was awful!!!

    Thanks heaps, Nick
    Win A FREE iPhone!

    Tired from working? Funny Things! Laugh the stress away
    Some Funny Jokes | Funny Videos


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
  •