Skip to content

Commit 057ef70

Browse files
authored
Update Str.php
1 parent d432ef9 commit 057ef70

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Utils/Str.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44

55
class Str
66
{
7+
// string to hex
8+
public static function stringToHex(?string $string)
9+
{
10+
if (!$string) {
11+
return null;
12+
}
13+
14+
$hex = '';
15+
for ($i=0; $i < strlen($string); $i++){
16+
$ord = ord($string[$i]);
17+
$hexCode = dechex($ord);
18+
$hex .= substr('0'.$hexCode, -2);
19+
}
20+
21+
return $hex;
22+
}
23+
24+
// hex to string
25+
public static function hexToString(?string $hex)
26+
{
27+
if (!$hex) {
28+
return null;
29+
}
30+
31+
$string = '';
32+
for ($i=0; $i < strlen($hex); $i+=2){
33+
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
34+
}
35+
36+
return $string;
37+
}
38+
739
// email
840
public static function maskEmail(?string $email = null): ?string
941
{

0 commit comments

Comments
 (0)