strip_tags() … replace tags by space rather than deleting them

前端 未结 10 964
日久生厌
日久生厌 2020-12-08 06:36

Do you know how to replace html tags with a space character using php?

If I display

strip_tags(\'

Foo

bar\');

相关标签:
10条回答
  • 2020-12-08 07:24

    Preg replace is fine most of the cases, there is that one corner case, as discussed here. This should work for both:

    strip_tags(str_replace('<', ' <', $str));

    Adding a space before any tag is valid in HTML. It too has some caveats like if your text has "<" for some reason and don't want to add space before it.

    0 讨论(0)
  • 2020-12-08 07:28
    $string      = '<h1>Foo</h1>bar';
    $spaceString = str_replace( '<', ' <',$string );
    $doubleSpace = strip_tags( $spaceString );
    $singleSpace = str_replace( '  ', ' ', $doubleSpace );
    
    0 讨论(0)
  • 2020-12-08 07:28

    try this:

    $str = '<h1>Foo</h1>bar';
    echo trim(preg_replace('/<[^>]*>/', ' ', $str));
    
    0 讨论(0)
  • 2020-12-08 07:28

    First do a str_replace

    $string = '<h1>Foo</h1>bar' 
    strip_tags(str_replace('</h1>', ' ',$string));
    
    0 讨论(0)
提交回复
热议问题