Code Duplication    Length = 58-59 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

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