1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Entities; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* \addtogroup Utility-classes Utility class |
23
|
|
|
* \brief Static methods grouped by class and context. |
24
|
|
|
* @{ |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* \class Text |
29
|
|
|
* \brief Contains text helper methods |
30
|
|
|
*/ |
31
|
|
|
class Text |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* \brief Get hashtag contained in a string. |
36
|
|
|
* \details Check hashtags in a string using regex. |
37
|
|
|
* All valid hashtags will be returned in an array. |
38
|
|
|
* [Credis to trante](http://stackoverflow.com/questions/3060601/retrieve-all-hashtags-from-a-tweet-in-a-php-function). |
39
|
|
|
* @param $string The string to check for hashtags. |
40
|
|
|
* @return An array of valid hashtags, can be empty. |
41
|
|
|
*/ |
42
|
|
|
public static function getHashtags(string $string) : array |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
// Use regex to check |
46
|
|
|
if (preg_match_all("/(#\w+)/u", $string, $matches) != 0) { |
47
|
|
|
$hashtagsArray = array_count_values($matches[0]); |
48
|
|
|
|
49
|
|
|
$hashtags = array_keys($hashtagsArray); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Return an array of hashtags |
53
|
|
|
return $hashtags ?? []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// http://www.mendoweb.be/blog/php-convert-string-to-camelcase-string/<Paste> |
57
|
|
|
public static function camelCase($str, array $noStrip = []) |
58
|
|
|
{ |
59
|
|
|
// non-alpha and non-numeric characters become spaces |
60
|
|
|
$str = preg_replace('/[^a-z0-9' . implode("", $noStrip) . ']+/i', ' ', $str); |
61
|
|
|
$str = trim($str); |
62
|
|
|
// uppercase the first character of each word |
63
|
|
|
$str = ucwords($str); |
64
|
|
|
$str = str_replace(" ", "", $str); |
65
|
|
|
$str = lcfirst($str); |
66
|
|
|
|
67
|
|
|
return $str; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* \brief Remove html formattation from telegram usernames in string. |
72
|
|
|
* \details Remove the $modificator html formattation from a message containing telegram username, to let the user click them. |
73
|
|
|
* @param $string to parse. |
74
|
|
|
* @param $tag Formattation tag to remove. |
75
|
|
|
* @return The string, modified if there are usernames. Otherwise $string. |
76
|
|
|
*/ |
77
|
|
|
public static function removeUsernameFormattation(string $string, string $tag) : string |
78
|
|
|
{ |
79
|
|
|
|
80
|
|
|
// Check if there are usernames in string using regex |
81
|
|
|
if (preg_match_all('/(@\w+)/u', $string, $matches) != 0) { |
82
|
|
|
$usernamesArray = array_count_values($matches[0]); |
83
|
|
|
|
84
|
|
|
$usernames = array_keys($usernamesArray); |
85
|
|
|
|
86
|
|
|
// Count how many username we've got |
87
|
|
|
$count = count($usernames); |
88
|
|
|
|
89
|
|
|
// Delimitator to make the formattation start |
90
|
|
|
$delimitator_start = '<' . $tag . '>'; |
91
|
|
|
|
92
|
|
|
// and to make it end |
93
|
|
|
$delimitator_end = '</' . $tag . '>'; |
94
|
|
|
|
95
|
|
|
// For each username |
96
|
|
|
for ($i = 0; $i !== $count; $i++) { |
97
|
|
|
// Put the Delimitator_end before the username and the start one after it |
98
|
|
|
$string = str_replace($usernames[$i], $delimitator_end . $usernames[$i] . $delimitator_start, $string); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Return the string, modified or not |
103
|
|
|
return $string; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** @} */ |
107
|
|
|
} |
108
|
|
|
|