matecat /
whole-text-finder
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Matecat\Finder\Helper; |
||
| 4 | |||
| 5 | class Strings |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @param string $string |
||
| 9 | * |
||
| 10 | * @return string |
||
| 11 | */ |
||
| 12 | public static function cutNbsps($string) |
||
| 13 | { |
||
| 14 | return str_replace([' ', ' '], ' ', $string); |
||
| 15 | } |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param string $string |
||
| 19 | * |
||
| 20 | * @return string |
||
| 21 | */ |
||
| 22 | public static function htmlEntityDecode($string) |
||
| 23 | { |
||
| 24 | return html_entity_decode($string, ENT_QUOTES|ENT_XHTML, 'UTF-8'); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string $string |
||
| 29 | * |
||
| 30 | * @return array|bool|string[]|null |
||
| 31 | */ |
||
| 32 | public static function split($string) |
||
| 33 | { |
||
| 34 | return mb_str_split($string); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param int $length |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | public static function token($length = 8) |
||
| 43 | { |
||
| 44 | $key = ''; |
||
| 45 | $keys = array_merge(range(0, 9), range('a', 'z')); |
||
| 46 | |||
| 47 | for ($i = 0; $i < $length; $i++) { |
||
| 48 | $key .= $keys[array_rand($keys)]; |
||
| 49 | } |
||
| 50 | |||
| 51 | return $key; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $string |
||
| 56 | * |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | public static function isMultibyte($string) |
||
| 60 | { |
||
| 61 | return ((strlen($string) - mb_strlen($string)) > 0); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param $needle |
||
| 66 | * @param $haystack |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public static function contains($needle, $haystack) |
||
| 71 | { |
||
| 72 | if(empty($haystack)){ |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | return strpos($haystack, $needle) !== false; |
||
| 77 | } |
||
| 78 | |||
| 79 | public static function firstChar($string) |
||
| 80 | { |
||
| 81 | return $string[0]; |
||
| 82 | } |
||
| 83 | |||
| 84 | public static function lastChar($string) |
||
| 85 | { |
||
| 86 | return $string[strlen($string) - 1]; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $string |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public static function protectHTMLTags($string) |
||
| 94 | { |
||
| 95 | preg_match_all('/<(.*?)>|<(.*?)>/sm', $string, $matches); |
||
| 96 | |||
| 97 | if(!empty($matches[0])){ |
||
| 98 | foreach ($matches[0] as $index => $element){ |
||
| 99 | |||
| 100 | $tag = explode(" ", $element); |
||
| 101 | $tag = str_replace(["<", ">", "<", ">", "/"], "", $tag[0]); |
||
| 102 | |||
| 103 | // opening tags |
||
| 104 | if(!self::contains("/", $element)){ |
||
| 105 | |||
| 106 | $tagMatch = false; |
||
| 107 | |||
| 108 | // check for the closing tag |
||
| 109 | for($i = ($index+1); $i < count($matches[0]); $i++){ |
||
|
0 ignored issues
–
show
|
|||
| 110 | $nextElement = $matches[0][$i] ?? null; |
||
| 111 | |||
| 112 | if($nextElement === null){ |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | $nextTag = explode(" ", $nextElement); |
||
| 117 | $nextTag = str_replace(["<", ">", "<", ">", "/"], "", $nextTag[0]); |
||
| 118 | |||
| 119 | if($nextTag === $tag){ |
||
| 120 | $tagMatch = true; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | if($tagMatch === false){ |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | // self closing tag |
||
| 129 | else { |
||
| 130 | $closingTag = explode(" ", $element); |
||
| 131 | $closingTag = str_replace(["<", ">", "<", ">"], "", $closingTag[0]); |
||
| 132 | |||
| 133 | if(empty($closingTag)){ |
||
| 134 | continue; |
||
| 135 | } |
||
| 136 | |||
| 137 | if(!(self::firstChar($closingTag) === "/" or self::lastChar($closingTag) === "/")){ |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $charMap = self::charMap(); |
||
| 143 | $protectedTag = str_replace(["<", ">", "<", ">"], [$charMap["<"], $charMap[">"], $charMap["<"], $charMap[">"]], $element); |
||
| 144 | $string = str_replace($element, $protectedTag, $string); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return $string; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $string |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public static function unprotectHTMLTags($string) |
||
| 156 | { |
||
| 157 | $charMap = self::charMap(); |
||
| 158 | $string = str_replace([$charMap["<"], $charMap[">"], $charMap["<"], $charMap[">"]], ["<", ">", "<", ">"], $string); |
||
| 159 | |||
| 160 | return $string; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | private static function charMap() |
||
| 167 | { |
||
| 168 | return [ |
||
| 169 | "<" => "ʃʃʃʃ", |
||
| 170 | ">" => "¶¶¶¶", |
||
| 171 | "<" => "ɑɑɑɑ", |
||
| 172 | ">" => "ʒʒʒʒ", |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | } |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: