Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts

    preg_replace not replacing all instances...what am l missing?

    Hi folks, l'm trying to work with a little preg replace script to change the SRC of images posted on one of my wordpress blogs. Basically what l'm trying to do is have the preg_match() function strip out the width and height attributes from the image(s) and alter the SRC attribute to point to a timthumb.php resizing script.


    PHP Code:
    $content '<img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-13.jpg" alt="" title="pics-(13)" width="820" height="668" class="alignnone size-full wp-image-64" />';

    $content .= '<img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-12.jpg" alt="" title="pics-(12)" width="820" height="757" class="alignnone size-full wp-image-63" />';


    $content_fixed preg_replace('/<img(.*)src=(\'|")(.*)(\'|")(.*)width=(\'|")(.*)(\'|")(.*)height=(\'|")(.*)(\'|")(.*)>/ims'"<img$1src=\"/photoblog/wp-content/themes/photoblog/scripts/timthumb.php?zc=0&w=580&src=$3$11\" /><br />"$content);


    echo 
    $content '<br />----------------<br />' $content_fixed
    Outputs:
    Code:
    <img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-13.jpg" alt="" title="pics-(13)" width="820" height="668" class="alignnone size-full wp-image-64" />
    
    <img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-12.jpg" alt="" title="pics-(12)" width="820" height="757" class="alignnone size-full wp-image-63" />
    
    ----------------
    
    <img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-13.jpg" alt="" title="pics-(13)" width="820" height="668" class="alignnone size-full wp-image-64" />
    
    <img src="/photoblog/wp-content/themes/photoblog/scripts/timthumb.php?zc=0&w=580&src=http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-12.jpg" alt="" title="pics-(12)757" class="alignnone size-full wp-image-63" /><br />
    For some reason it's only applying to the last IMG tag, and l'm not sure why. Can anyone that's a little more seasoned with preg_replace help me out a bit and tell me what l'm doing wrong, and how l can fix it?
    Last edited by Coastal Web; 03-31-2010 at 01:23 AM. Reason: error in title

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    You say preg_match but your using preg_replace??

    And use preg_match_all(); or preg_replace_all();
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    There is no preg_replace_all function. I'd say making the expression not greedy might help:

    Code:
    These:
    
    (.*)
    
    ought be:
    
    (.*?)

  • #4
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Ooops, sorry yeah I meant preg replace, my mind was thinking about the end of the work day I guess. I don't think there is a preg_replace_all, preg_replace is supposed to be greedy by default.

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Coastal Web View Post
    preg_replace is supposed to be greedy by default.
    Which is most likely the cause of your problem.

  • #6
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Mattf, greedy as in its supposed to change all found instances unless a 4th varfable is given.

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Coastal Web View Post
    Mattf, greedy as in its supposed to change all found instances unless a 4th varfable is given.
    That isn't what greedy does or means. It will go to the last match it finds instead of stopping at each specific instance. Just do as I suggested above. You can argue the toss on semantics if that doesn't work.

  • Users who have thanked MattF for this post:

    Coastal Web (03-31-2010)

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    oh my bad no preg_replace_all i was thinking about preg_replace_callback sorry
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #9
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by MattF View Post
    That isn't what greedy does or means. It will go to the last match it finds instead of stopping at each specific instance. Just do as I suggested above. You can argue the toss on semantics if that doesn't work.
    Hi MattF,

    Thanks for the clarification, you are correct l mis-understood what greedy meant. I've tried your solution of changing the (.*)'s to (.*?) doesn't seem to solve the problem but outputs things differently.

    I'm currently using:
    $content = preg_replace('/<img(.*?)src=(\'|")(.*?)(\'|")(.*?)>/ims', "<img$1src=\"/photoblog/wp-content/themes/photoblog/scripts/timthumb.php?zc=0&w=580&src=$3\" alt=\"\"><br />", get_the_content());

    Which basically just strips everything after the src attribute and seems to work; I'd rather have it remove the height/width attributes and keep the rest, but l just can't seem to get it to work.
    Last edited by Coastal Web; 03-31-2010 at 03:46 AM.

  • #10
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    I found this little snippet and am using it after the fact to remove the height/width attributes from all the images:

    $content = preg_replace("!(<img)([^<>]+)(/>)!e","'\\1'.preg_replace('!(height|width)=\"[^\"]*\"!i','',\"\\2\").'\\3'", $content);


    I'm not nearly good enough at regular expressions to understand what's going on, but it seems to do the trick.

    Thanks to all that have helped.

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Give that a whirl. It's untested, but should give you what you're after, I believe, (provided the format is consistent). $1 and $2 are the bits you want.

    Code:
    '#<img (.+?)(?:width="[^"]+" height="[^"]+")([^\>]+?)/>#ims'
    That'll grab everything except for the height and width tags.
    Last edited by MattF; 03-31-2010 at 04:32 AM.


  •  

    Posting Permissions

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