PHP - remove

后端 未结 7 2457
梦如初夏
梦如初夏 2020-11-27 15:15

Hey, I need to delete all images from a string and I just can\'t find the right way to do it.

Here is what I tryed, but it doesn\'t work:

preg_replac         


        
相关标签:
7条回答
  • 2020-11-27 15:21
    $this->load->helper('security');
    $h=mysql_real_escape_string(strip_image_tags($comment));
    

    If user inputs

    <img src="#">
    

    In the database table just insert character this #

    Works for me

    0 讨论(0)
  • 2020-11-27 15:25

    Sean it works fine i've just used this code

    $content = preg_replace("/<img[^>]+\>/i", " ", $content); 
    echo $content;
    

    //the result it's only the plain text. It works!!!

    0 讨论(0)
  • 2020-11-27 15:29

    I would suggest using the strip_tags method.

    0 讨论(0)
  • 2020-11-27 15:32

    I wanted to display the first 300 words of a news story as a preview which unfortunately meant that if a story had an image within the first 300 words then it was displayed in the list of previews which really messed with my layout. I used the above code to hide all of the images from the string taken from my database and it works wonderfully!

    $news = $row_latest_news ['content'];
    $news = preg_replace("/<img[^>]+\>/i", "", $news); 
    if (strlen($news) > 300){
    echo substr($news, 0, strpos($news,' ',300)).'...';
    } 
    else { 
    echo $news; 
    }
    
    0 讨论(0)
  • 2020-11-27 15:32

    simply use the form_validation class of codeigniter:

    strip_image_tags($str).
    
    $this->load->library('form_validation');
    $this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');
    
    0 讨论(0)
  • 2020-11-27 15:41

    Try dropping the \ in front of the >.

    Edit: I just tested your regex and it works fine. This is what I used:

    <?
        $content = "this is something with an <img src=\"test.png\"/> in it.";
        $content = preg_replace("/<img[^>]+\>/i", "(image) ", $content); 
        echo $content;
    ?>
    

    The result is:

    this is something with an (image)  in it.
    
    0 讨论(0)
提交回复
热议问题