How to remove
tags and more from a string?

前端 未结 7 1657
一生所求
一生所求 2020-12-04 00:08

I need to strip all
and all \'quotes\' (\") and all \'ands\' (&) and replace them with a space only ...

How c

相关标签:
7条回答
  • 2020-12-04 00:49

    This worked for me, to remove <br/> :

    (&gt; is recognised whereas > isn't)

    $temp2 = str_replace('&lt;','', $temp);
    
    // echo ($temp2);
    
    $temp2 = str_replace('/&gt;','', $temp2);
    
    // echo ($temp2);
    
    $temp2 = str_replace('br','', $temp2);
    
    echo ($temp2);
    
    0 讨论(0)
  • 2020-12-04 00:53

    To remove all permutations of br:

    <br> <br /> <br/> <br   >
    

    check out the user contributed strip_only() function in

    http://www.php.net/strip_tags

    The "Use the DOM instead of replacing" caveat is always correct, but if the task is really limited to these three characters, this should be o.k.

    0 讨论(0)
  • 2020-12-04 00:56

    You can use str_replace like this:

      str_replace("<br/>", " ", $orig );
    

    preg_replace etc uses regular expressions and that may not be what you want.

    0 讨论(0)
  • 2020-12-04 00:58

    If str_replace() isnt working for you, then something else must be wrong, because

    $string = 'A string with <br/> & "double quotes".';
    $string = str_replace(array('<br/>', '&', '"'), ' ', $string);
    echo $string;
    

    outputs

    A string with      double quotes .
    

    Please provide an example of your input string and what you expect it to look like after filtering.

    0 讨论(0)
  • 2020-12-04 01:04

    Try this:

    $description = preg_replace('/<br \/>/iU', '', $description);
    
    0 讨论(0)
  • 2020-12-04 01:07

    To manipulate HTML it is generally a good idea to use a DOM aware tool instead of plain text manipulation tools (think for example what will happen if you enounter variants like <br/>, <br /> with more than one space, or even <br> or <BR/>, which altough illegal are sometimes used). See for example here: http://sourceforge.net/projects/simplehtmldom/

    0 讨论(0)
提交回复
热议问题