How limit html tags in PHP

Discussion in 'PHP' started by love_bug, Oct 8, 2007.

  1. #1
    Hi is there any function that can limit or remove more than 1 img tags in a string?

    <?php
    $string = "<img src=1> this is text <img src=2> <img src=3> here\'s some text <img src=4>";
    echo limitImage($string);
    
    function limitImage {
    // Some function to do remove more than 1 img tags ..
    }
    ?>
    PHP:
    thanks..
     
    love_bug, Oct 8, 2007 IP
  2. chicagoweb

    chicagoweb Peon

    Messages:
    30
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    chicagoweb, Oct 8, 2007 IP
  3. love_bug

    love_bug Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    thanks.. but i think it will remove all the image tags.. i want to limit it.
     
    love_bug, Oct 8, 2007 IP
  4. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Find out how many img tags there are in the string.

    If there is more than one, change the first one to something else, strip the rest of the image-tags and put back in the first one.

    I cant write any code right now, I have a baby watching Totoro in my arm, but the above should be pretty easy.
     
    tamen, Oct 8, 2007 IP
  5. love_bug

    love_bug Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    i am a novice ... pls hlp.. :(
     
    love_bug, Oct 8, 2007 IP
  6. digipren

    digipren Active Member

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    You can use preg_replace()
    Please write what you exactly want "echo limitImage($string);" to show with your example if you want more help.
     
    digipren, Oct 8, 2007 IP
  7. love_bug

    love_bug Member

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Hi..
    I want to find all image tags in the string, keep the first image (img tag) and remove all others. basically to show just one image..if string contains 2 or more images..
     
    love_bug, Oct 8, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    
    function limitImage($string)
    {
    	$string = preg_split('/(<img[^>]+>)/i', $string, 2, PREG_SPLIT_DELIM_CAPTURE);
    	return $string[0] . $string[1] . strip_tags($string[2]);
    }
    
    
    PHP:
     
    nico_swd, Oct 9, 2007 IP