Code Duplication    Length = 58-59 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 5053-5111 (lines=59) @@
5050
   *
5051
   * @return false|string A sub-string,<br />or <strong>false</strong> if needle is not found.
5052
   */
5053
  public static function stristr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false)
5054
  {
5055
    $haystack = (string)$haystack;
5056
    $needle = (string)$needle;
5057
    $before_needle = (bool)$before_needle;
5058
5059
    if (!isset($haystack[0], $needle[0])) {
5060
      return false;
5061
    }
5062
5063
    if ($encoding !== 'UTF-8') {
5064
      $encoding = self::normalize_encoding($encoding, 'UTF-8');
5065
    }
5066
5067
    if ($cleanUtf8 === true) {
5068
      // "\mb_strpos" and "\iconv_strpos" returns wrong position,
5069
      // if invalid characters are found in $haystack before $needle
5070
      $needle = self::clean($needle);
5071
      $haystack = self::clean($haystack);
5072
    }
5073
5074
    if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) {
5075
      self::checkForSupport();
5076
    }
5077
5078
    if (
5079
        $encoding !== 'UTF-8'
5080
        &&
5081
        self::$SUPPORT['mbstring'] === false
5082
    ) {
5083
      trigger_error('UTF8::stristr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING);
5084
    }
5085
5086
    if (self::$SUPPORT['mbstring'] === true) {
5087
      return \mb_stristr($haystack, $needle, $before_needle, $encoding);
5088
    }
5089
5090
    if (
5091
        $encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings
5092
        &&
5093
        self::$SUPPORT['intl'] === true
5094
        &&
5095
        Bootup::is_php('5.4') === true
5096
    ) {
5097
      return \grapheme_stristr($haystack, $needle, $before_needle);
5098
    }
5099
5100
    preg_match('/^(.*?)' . preg_quote($needle, '/') . '/usi', $haystack, $match);
5101
5102
    if (!isset($match[1])) {
5103
      return false;
5104
    }
5105
5106
    if ($before_needle) {
5107
      return $match[1];
5108
    }
5109
5110
    return self::substr($haystack, self::strlen($match[1]));
5111
  }
5112
5113
  /**
5114
   * Get the string length, not the byte-length!
@@ 5766-5823 (lines=58) @@
5763
   *
5764
   * @return string|false A sub-string,<br />or <strong>false</strong> if needle is not found.
5765
   */
5766
  public static function strstr($haystack, $needle, $before_needle = false, $encoding = 'UTF-8', $cleanUtf8 = false)
5767
  {
5768
    $haystack = (string)$haystack;
5769
    $needle = (string)$needle;
5770
5771
    if (!isset($haystack[0], $needle[0])) {
5772
      return false;
5773
    }
5774
5775
    if ($cleanUtf8 === true) {
5776
      // "\mb_strpos" and "\iconv_strpos" returns wrong position,
5777
      // if invalid characters are found in $haystack before $needle
5778
      $needle = self::clean($needle);
5779
      $haystack = self::clean($haystack);
5780
    }
5781
5782
    if ($encoding !== 'UTF-8') {
5783
      $encoding = self::normalize_encoding($encoding, 'UTF-8');
5784
    }
5785
5786
    if (!isset(self::$SUPPORT['already_checked_via_portable_utf8'])) {
5787
      self::checkForSupport();
5788
    }
5789
5790
    if (
5791
        $encoding !== 'UTF-8'
5792
        &&
5793
        self::$SUPPORT['mbstring'] === false
5794
    ) {
5795
      trigger_error('UTF8::strstr() without mbstring cannot handle "' . $encoding . '" encoding', E_USER_WARNING);
5796
    }
5797
5798
    if (self::$SUPPORT['mbstring'] === true) {
5799
      return \mb_strstr($haystack, $needle, $before_needle, $encoding);
5800
    }
5801
5802
    if (
5803
        $encoding === 'UTF-8' // INFO: "grapheme_stripos()" can't handle other encodings
5804
        &&
5805
        self::$SUPPORT['intl'] === true
5806
        &&
5807
        Bootup::is_php('5.4') === true
5808
    ) {
5809
      return \grapheme_strstr($haystack, $needle, $before_needle);
5810
    }
5811
5812
    preg_match('/^(.*?)' . preg_quote($needle, '/') . '/us', $haystack, $match);
5813
5814
    if (!isset($match[1])) {
5815
      return false;
5816
    }
5817
5818
    if ($before_needle) {
5819
      return $match[1];
5820
    }
5821
5822
    return self::substr($haystack, self::strlen($match[1]));
5823
  }
5824
5825
  /**
5826
   * Unicode transformation for case-less matching.