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

前端 未结 10 986
日久生厌
日久生厌 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:23

    I helped my self with example from user40521, but I made a function with same api like php's strip_tags, it does not use multiple variables, and it also makes a trim, so a single whitespace is removed from start / end.

    /**
     * @param string $string
     * @param string|null $allowable_tags
     * @return string
     */
    function strip_tags_with_whitespace($string, $allowable_tags = null)
    {
        $string = str_replace('<', ' <', $string);
        $string = strip_tags($string, $allowable_tags);
        $string = str_replace('  ', ' ', $string);
        $string = trim($string);
    
        return $string;
    }
    

提交回复
热议问题