-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHtmlEntitiesEncoder.php
62 lines (51 loc) · 1.51 KB
/
HtmlEntitiesEncoder.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
<?php
declare(strict_types=1);
namespace VStelmakh\UrlHighlight\Encoder;
use VStelmakh\UrlHighlight\Util\CaseInsensitiveSet;
/**
* Could be used when input string expected to be html entity encoded
* Use HtmlSpecialcharsEncoder for html escaped string (less regex operations)
*/
class HtmlEntitiesEncoder implements EncoderInterface
{
/**
* Decode html encoded string
*
* @param string $string
* @return string
*/
public function decode(string $string): string
{
return html_entity_decode($string, ENT_QUOTES + ENT_HTML5);
}
/**
* Return regex to match: char or html entity or numeric character reference
*
* @param string $char
* @param string $delimiter
* @return string
*/
public function getEncodedCharRegex(string $char, string $delimiter = '/'): string
{
if (empty($char)) {
return '';
}
$variations = new CaseInsensitiveSet([preg_quote($char, $delimiter)]);
$encodedChar = htmlentities($char, ENT_QUOTES + ENT_HTML5);
$variations->add(preg_quote($encodedChar, $delimiter));
$charCodeDec = \mb_ord($char);
$variations->add('�*' . $charCodeDec . ';');
$charCodeHex = dechex($charCodeDec);
$variations->add('�*' . $charCodeHex . ';');
return implode('|', $variations->toArray());
}
/**
* Match all chars
*
* @return string[]|null
*/
public function getSupportedChars(): ?array
{
return null;
}
}