SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Strip image tags by attribute

    OK need help to strip out image tags basically what i want to do is if image tag contains something like width="10" then the whole img tag will be removed so far i manage to do it like this
    PHP Code:
    $string preg_replace("/<img width\=[\"]10[\"][^>]+>/i","",$string); 
    But with this the width attribute needs to be right after <img
    example
    like this it works
    HTML Code:
    <img width="10" src="example" />
    like this it wont work
    HTML Code:
    <img src="example" width="10" />
    any ideas

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You'll to forgive my poor RegExp skills...

    PHP Code:
    <?php
    $string 
    preg_replace('~(?<=<img).*?(width="10").*?(?=/>)~i'null$string);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    '/<img[^>]+width="10"[^>]*>/i'


    Be aware this isn't robust. title="apples > oranges" will break it. Inline javascript is a common source of that stuff.

  4. #4
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    You'll to forgive my poor RegExp skills...

    PHP Code:
    <?php
    $string 
    preg_replace('~(?<=<img).*?(width="10").*?(?=/>)~i'null$string);
    ?>
    Dont know why but it removes all images

  5. #5
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    '/<img[^>]+width="10"[^>]*>/i'


    Be aware this isn't robust. title="apples > oranges" will break it. Inline javascript is a common source of that stuff.
    Dosent work for me maybe i did something wrong?

  6. #6
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kristallaju View Post
    Dosent work for me maybe i did something wrong?
    Sorry works fine thank you you saved my day


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
  •