Code Duplication    Length = 58-59 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 5073-5131 (lines=59) @@
5070
   *
5071
   * @return false|string A sub-string,<br />or <strong>false</strong> if needle is not found.
5072
   */
5073
  public static function stristr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false)
5074
  {
5075
    $haystack = (string)$haystack;
5076
    $needle = (string)$needle;
5077
    $before_needle = (bool)$before_needle;
5078
5079
    if (!isset($haystack[0], $needle[0])) {
5080
      return false;
5081
    }
5082
5083
    if ($encoding !== 'UTF-8') {
5084
      $encoding = self::normalize_encoding($encoding, 'UTF-8');
5085
    }
5086
5087
    if ($cleanUtf8 === true) {
5088
      // "\mb_strpos" and "\iconv_strpos" returns wrong position,
5089
      // if invalid characters are found in $haystack before $needle
5090
      $needle = self::clean($needle);
5091
      $haystack = self::clean($haystack);
5092
    }
5093
5094
    if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) {
5095
      self::checkForSupport();
5096
    }
5097
5098
    if (
5099
        $encoding !== 'UTF-8'
5100
        &&
5101
        self::$SUPPORT['mbstring'] === false
5102
    ) {
5103
      trigger_error('UTF8::stristr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING);
5104
    }
5105
5106
    if (self::$SUPPORT['mbstring'] === true) {
5107
      return \mb_stristr($haystack, $needle, $before_needle, $encoding);
5108
    }
5109
5110
    if (
5111
        $encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings
5112
        &&
5113
        self::$SUPPORT['intl'] === true
5114
        &&
5115
        Bootup::is_php('5.4') === true
5116
    ) {
5117
      return \grapheme_stristr($haystack, $needle, $before_needle);
5118
    }
5119
5120
    preg_match('/^(.*?)' . preg_quote($needle, '/') . '/usi', $haystack, $match);
5121
5122
    if (!isset($match[1])) {
5123
      return false;
5124
    }
5125
5126
    if ($before_needle) {
5127
      return $match[1];
5128
    }
5129
5130
    return self::substr($haystack, self::strlen($match[1]));
5131
  }
5132
5133
  /**
5134
   * Get the string length, not the byte-length!
@@ 5786-5843 (lines=58) @@
5783
   *
5784
   * @return string|false A sub-string,<br />or <strong>false</strong> if needle is not found.
5785
   */
5786
  public static function strstr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false)
5787
  {
5788
    $haystack = (string)$haystack;
5789
    $needle = (string)$needle;
5790
5791
    if (!isset($haystack[0], $needle[0])) {
5792
      return false;
5793
    }
5794
5795
    if ($cleanUtf8 === true) {
5796
      // "\mb_strpos" and "\iconv_strpos" returns wrong position,
5797
      // if invalid characters are found in $haystack before $needle
5798
      $needle = self::clean($needle);
5799
      $haystack = self::clean($haystack);
5800
    }
5801
5802
    if ($encoding !== 'UTF-8') {
5803
      $encoding = self::normalize_encoding($encoding, 'UTF-8');
5804
    }
5805
5806
    if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) {
5807
      self::checkForSupport();
5808
    }
5809
5810
    if (
5811
        $encoding !== 'UTF-8'
5812
        &&
5813
        self::$SUPPORT['mbstring'] === false
5814
    ) {
5815
      trigger_error('UTF8::strstr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING);
5816
    }
5817
5818
    if (self::$SUPPORT['mbstring'] === true) {
5819
      return \mb_strstr($haystack, $needle, $before_needle, $encoding);
5820
    }
5821
5822
    if (
5823
        $encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings
5824
        &&
5825
        self::$SUPPORT['intl'] === true
5826
        &&
5827
        Bootup::is_php('5.4') === true
5828
    ) {
5829
      return \grapheme_strstr($haystack, $needle, $before_needle);
5830
    }
5831
5832
    preg_match('/^(.*?)' . preg_quote($needle, '/') . '/us', $haystack, $match);
5833
5834
    if (!isset($match[1])) {
5835
      return false;
5836
    }
5837
5838
    if ($before_needle) {
5839
      return $match[1];
5840
    }
5841
5842
    return self::substr($haystack, self::strlen($match[1]));
5843
  }
5844
5845
  /**
5846
   * Unicode transformation for case-less matching.