Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Last active January 4, 2022 10:38

Revisions

  1. mcaskill revised this gist Nov 24, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion composer.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    {
    "name": "mcaskill/php-strip-html",
    "version": "1.0.0",
    "description": "Strip HTML and PHP tags from a string.",
    "license": "MIT",
    "authors": [
    @@ -13,6 +12,11 @@
    "keywords": [
    "function"
    ],
    "extra": {
    "branch-alias": {
    "dev-master": "1.x-dev"
    }
    },
    "require": {
    "php": ">=5.4.0"
    },
  2. mcaskill revised this gist Feb 23, 2017. 3 changed files with 7 additions and 19 deletions.
    10 changes: 5 additions & 5 deletions Function.Strip-HTML.php
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    <?php

    if ( ! function_exists('strip_html') ) :
    if (!function_exists('strip_html')) {
    /**
    * Strip HTML and PHP tags from a string.
    *
    * @param string $str The input string.
    * @return string Returns the stripped string.
    * @param string $str The input string.
    * @return string Returns the stripped string.
    */
    function strip_html( $str )
    function strip_html($str)
    {
    $str = html_entity_decode($str);

    @@ -38,4 +38,4 @@ function strip_html( $str )

    return $str;
    }
    endif;
    }
    14 changes: 0 additions & 14 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -29,20 +29,6 @@ Returns the stripped string.
    $ composer require mcaskill/php-strip-html
    ```

    ```json
    {
    "repositories": [
    {
    "type": "git",
    "url": "https://gist.github.com/***.git"
    }
    ],
    "require": {
    "mcaskill/php-strip-html": "dev-master"
    }
    }
    ```

    ### Without Composer

    Why are you not using [composer](http://getcomposer.org/)? Download `Function.Strip-HTML.php` from the gist and save the file into your project path somewhere.
    2 changes: 2 additions & 0 deletions composer.json
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    {
    "name": "mcaskill/php-strip-html",
    "version": "1.0.0",
    "description": "Strip HTML and PHP tags from a string.",
    "license": "MIT",
    "authors": [
    {
    "name": "Chauncey McAskill",
  3. mcaskill created this gist May 31, 2016.
    41 changes: 41 additions & 0 deletions Function.Strip-HTML.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <?php

    if ( ! function_exists('strip_html') ) :
    /**
    * Strip HTML and PHP tags from a string.
    *
    * @param string $str The input string.
    * @return string Returns the stripped string.
    */
    function strip_html( $str )
    {
    $str = html_entity_decode($str);

    // Strip HTML
    $str = preg_replace('#<br[^>]*?>#siu', "\n", $str);
    $str = preg_replace(
    [
    '#<head[^>]*?>.*?</head>#siu',
    '#<style[^>]*?>.*?</style>#siu',
    '#<script[^>]*?.*?</script>#siu',
    '#<object[^>]*?.*?</object>#siu',
    '#<embed[^>]*?.*?</embed>#siu',
    '#<applet[^>]*?.*?</applet>#siu',
    '#<noframes[^>]*?.*?</noframes>#siu',
    '#<noscript[^>]*?.*?</noscript>#siu',
    '#<noembed[^>]*?.*?</noembed>#siu'
    ],
    '',
    $str
    );
    $str = strip_tags($str);

    // Trim whitespace
    $str = str_replace("\t", '', $str);
    $str = preg_replace('#\n\r|\r\n#', "\n", $str);
    $str = preg_replace('#\n{3,}#', "\n\n", $str);
    $str = trim($str);

    return $str;
    }
    endif;
    48 changes: 48 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # strip_html

    (PHP 5 >= 5.4)
    `strip_html` — Strip HTML and PHP tags from a string.

    ## Description

    ```php
    string strip_html( string $str )
    ```

    This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given `str`. Unlike [`strip_tags()`](https://php.net/strip-tags), this function is mindful of line-breaks and metadata elements (e.g., `<style>`).

    This function is useful for converting a HTML-rich string into a plain-text string for emails.

    ## Parameters

    - `str` — The input string.

    ## Return Values

    Returns the stripped string.

    ## Installation

    ### With Composer

    ```
    $ composer require mcaskill/php-strip-html
    ```

    ```json
    {
    "repositories": [
    {
    "type": "git",
    "url": "https://gist.github.com/***.git"
    }
    ],
    "require": {
    "mcaskill/php-strip-html": "dev-master"
    }
    }
    ```

    ### Without Composer

    Why are you not using [composer](http://getcomposer.org/)? Download `Function.Strip-HTML.php` from the gist and save the file into your project path somewhere.
    20 changes: 20 additions & 0 deletions composer.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    {
    "name": "mcaskill/php-strip-html",
    "description": "Strip HTML and PHP tags from a string.",
    "authors": [
    {
    "name": "Chauncey McAskill",
    "email": "chauncey@mcaskill.ca",
    "homepage": "https://github.com/mcaskill"
    }
    ],
    "keywords": [
    "function"
    ],
    "require": {
    "php": ">=5.4.0"
    },
    "autoload": {
    "files": ["Function.Strip-HTML.php"]
    }
    }