Skip to content

Instantly share code, notes, and snippets.

@Ellrion
Last active August 11, 2017 08:27
Show Gist options
  • Select an option

  • Save Ellrion/5810b9bb5593555a8348 to your computer and use it in GitHub Desktop.

Select an option

Save Ellrion/5810b9bb5593555a8348 to your computer and use it in GitHub Desktop.

Revisions

  1. Ellrion revised this gist Aug 11, 2017. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php

    if (!function_exists('ex')) {
    /**
    * Escape HTML entities in a string with exclusion some tags.
    @@ -14,10 +15,10 @@ function ex($str, array $excluded)
    $patterns = $tags = [];
    foreach ($excluded as $ex) {
    $allowed_tag_variants = [
    '<' . $ex . '>', //open tag
    '<' . $ex . '>', //open tag or single tag variant
    '</' . $ex . '>', //close tag
    '<' . $ex . ' />', //single tag variant one
    '<' . $ex . '>', //single tag variant two
    '<' . $ex . ' />', //single tag variant
    '<' . $ex . '/>', //single tag variant
    ];
    foreach ($allowed_tag_variants as $tag) {
    $patterns[] = '~' . preg_quote(e($tag), '~') . '~';
  2. Ellrion revised this gist Aug 11, 2017. 1 changed file with 19 additions and 10 deletions.
    29 changes: 19 additions & 10 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,30 @@
    <?php
    /**
    * use in blade template `{!! ex($message, ['br', 'strong']) !!}`
    */
    if (!function_exists('ex')) {
    /**
    * Escape HTML entities in a string with exclusion some tags.
    *
    * use in blade template `{!! ex($message, ['br', 'strong']) !!}`
    *
    * @param string $str
    * @param string[] $excluded
    * @return string
    */
    function ex($str, array $excluded)
    {
    $patterns = $tags = [];
    foreach ($excluded as $ex) {
    $open_tag = '<' . $ex . '>';
    $close_tag = '</' . $ex . '>';
    $patterns[] = '~' . preg_quote(e($open_tag), '~') . '~';
    $tags[] = $open_tag;
    $patterns[] = '~' . preg_quote(e($close_tag), '~') . '~';
    $tags[] = $close_tag;
    $allowed_tag_variants = [
    '<' . $ex . '>', //open tag
    '</' . $ex . '>', //close tag
    '<' . $ex . ' />', //single tag variant one
    '<' . $ex . '>', //single tag variant two
    ];
    foreach ($allowed_tag_variants as $tag) {
    $patterns[] = '~' . preg_quote(e($tag), '~') . '~';
    $tags[] = $tag;
    }
    }

    return preg_replace($patterns, $tags, e($str));
    }
    }

  3. Ellrion revised this gist Feb 10, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    <?php
    /**
    * use in blade template `{!! ex($message, ['br', 'strong']) !!}`
    */
    if (!function_exists('ex')) {
    function ex($str, array $excluded)
    {
  4. Ellrion created this gist Feb 10, 2016.
    18 changes: 18 additions & 0 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php
    if (!function_exists('ex')) {
    function ex($str, array $excluded)
    {
    $patterns = $tags = [];
    foreach ($excluded as $ex) {
    $open_tag = '<' . $ex . '>';
    $close_tag = '</' . $ex . '>';
    $patterns[] = '~' . preg_quote(e($open_tag), '~') . '~';
    $tags[] = $open_tag;
    $patterns[] = '~' . preg_quote(e($close_tag), '~') . '~';
    $tags[] = $close_tag;
    }

    return preg_replace($patterns, $tags, e($str));
    }
    }