SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: img replace

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    img replace

    Having multiple image scenarios, need to find a regex to find an image and replace the full tag with other tag...

    PHP Code:
    $a 'asdasdasd as asdasdas <img src="http://www.domain1.com/1.gif"> sdfsdf';
    // or
    $a 'asdasdasd as asdasdas <img class="right" src=http://www.domain1.com/1.gif /> sdfsdf';
    // or
    $a 'asdasdasd as asdasdas <img alt="image" src=http://www.domain1.com/1.gif> sdfsdf';
    // or
    $a "asdasdasd as asdasdas <img width='10' height='10' src='http://www.domain1.com/1.gif' alt='as' /> sdfsdf";
    // or
    $a preg_match('/(<img).*?src.*?(http://www.domain1.com/1.gif)/i>','<img src="http://www.domain2.com/11.gif">'$a);

    var_dump($a);
    // should be 'asdasdasd as asdasdas <img src="http://www.domain2.com/11.gif"> sdfsdf' 
    May you please improve my regex?

    Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've used these before - see if you can combine them:
    Code:
    $img_tag_pattern = "#\<img[^\>]+\>#i";
    
    $img_src_pattern = "#(= src=['\"]?.+[^\"]?)?src=[\"']?([^\"']+)#i";

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like

    PHP Code:
    $a preg_replace('|<img ([^>]*)src="?http://www.domain1.com/1.gif"? ([^>]*)>|i','<img src="http://www.domain2.com/11.gif">'$a); 
    but still not working...


    PHP Code:
     $a 'asdasdasd as asdasdas <img src="http://www.domain1.com/1.gif"> sdfsdf';
    // or
    $a 'asdasdasd as asdasdas <img class="right" src=http://www.domain1.com/1.gif /> sdfsdf';
    // or
    $a 'asdasdasd as asdasdas <img alt="image" src=http://www.domain1.com/1.gif> sdfsdf';
    // or
    $a "asdasdasd as asdasdas <img width='10' height='10' src='http://www.domain1.com/1.gif' alt='as' /> sdfsdf";
    // or
    $a preg_match('/(<img).*?src.*?(http://www.domain1.com/1.gif)/i>','<img src="http://www.domain2.com/11.gif">'$a);

    var_dump($a);
    // should be 'asdasdasd as asdasdas <img src="http://www.domain2.com/11.gif"> sdfsdf' 

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By other words

    What I need is find the img tag with the src=http://www.domain1.com/1.gif (this tag can have many alternatives) and replace <img> tag with the following:
    <img src="http://www.domain2.com/11.gif">

    Many thanks


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
  •