Blame view

vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/TextData/Search.php 2.23 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  <?php
  
  namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
  
  use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  
  class Search
  {
      /**
       * FIND (case sensitive search).
       *
       * @param mixed $needle The string to look for
       * @param mixed $haystack The string in which to look
       * @param mixed $offset Integer offset within $haystack to start searching from
       *
       * @return int|string
       */
      public static function sensitive($needle, $haystack, $offset = 1)
      {
          try {
              $needle = Helpers::extractString($needle);
              $haystack = Helpers::extractString($haystack);
              $offset = Helpers::extractInt($offset, 1, 0, true);
          } catch (CalcExp $e) {
              return $e->getMessage();
          }
  
          if (StringHelper::countCharacters($haystack) >= $offset) {
              if (StringHelper::countCharacters($needle) === 0) {
                  return $offset;
              }
  
              $pos = mb_strpos($haystack, $needle, --$offset, 'UTF-8');
              if ($pos !== false) {
                  return ++$pos;
              }
          }
  
          return Functions::VALUE();
      }
  
      /**
       * SEARCH (case insensitive search).
       *
       * @param mixed $needle The string to look for
       * @param mixed $haystack The string in which to look
       * @param mixed $offset Integer offset within $haystack to start searching from
       *
       * @return int|string
       */
      public static function insensitive($needle, $haystack, $offset = 1)
      {
          try {
              $needle = Helpers::extractString($needle);
              $haystack = Helpers::extractString($haystack);
              $offset = Helpers::extractInt($offset, 1, 0, true);
          } catch (CalcExp $e) {
              return $e->getMessage();
          }
  
          if (StringHelper::countCharacters($haystack) >= $offset) {
              if (StringHelper::countCharacters($needle) === 0) {
                  return $offset;
              }
  
              $pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8');
              if ($pos !== false) {
                  return ++$pos;
              }
          }
  
          return Functions::VALUE();
      }
  }