]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/LinkUtils.php
4 * Extract title from an HTML document.
6 * @param string $html HTML content where to look for a title.
8 * @return bool|string Extracted title if found, false otherwise.
10 function html_extract_title($html)
12 if (preg_match('!<title.*?>(.*?)</title>!is', $html, $matches)) {
13 return trim(str_replace("\n", '', $matches[1]));
19 * Determine charset from downloaded page.
21 * 1. HTTP headers (Content type).
22 * 2. HTML content page (tag <meta charset>).
23 * 3. Use a default charset (default: UTF-8).
25 * @param array $headers HTTP headers array.
26 * @param string $htmlContent HTML content where to look for charset.
27 * @param string $defaultCharset Default charset to apply if other methods failed.
29 * @return string Determined charset.
31 function get_charset($headers, $htmlContent, $defaultCharset = 'utf-8')
33 if ($charset = headers_extract_charset($headers)) {
37 if ($charset = html_extract_charset($htmlContent)) {
41 return $defaultCharset;
45 * Extract charset from HTTP headers if it's defined.
47 * @param array $headers HTTP headers array.
49 * @return bool|string Charset string if found (lowercase), false otherwise.
51 function headers_extract_charset($headers)
53 if (! empty($headers['Content-Type']) && strpos($headers['Content-Type'], 'charset=') !== false) {
54 preg_match('/charset="?([^; ]+)/i', $headers['Content-Type'], $match);
55 if (! empty($match[1])) {
56 return strtolower(trim($match[1]));
64 * Extract charset HTML content (tag <meta charset>).
66 * @param string $html HTML content where to look for charset.
68 * @return bool|string Charset string if found, false otherwise.
70 function html_extract_charset($html)
72 // Get encoding specified in HTML header.
73 preg_match('#<meta .*charset=["\']?([^";\'>/]+)["\']? */?>#Usi', $html, $enc);
74 if (!empty($enc[1])) {
75 return strtolower($enc[1]);
82 * Count private links in given linklist.
84 * @param array|Countable $links Linklist.
86 * @return int Number of private links.
88 function count_private($links)
91 foreach ($links as $link) {
92 if ($link['private']) {
101 * In a string, converts URLs to clickable links.
103 * @param string $text input string.
104 * @param string $redirector if a redirector is set, use it to gerenate links.
105 * @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not.
107 * @return string returns $text with all links converted to HTML links.
109 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
111 function text2clickable($text, $redirector = '', $urlEncode = true)
113 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
115 if (empty($redirector)) {
116 return preg_replace($regex, '<a href="$1">$1</a>', $text);
118 // Redirector is set, urlencode the final URL.
119 return preg_replace_callback(
121 function ($matches) use ($redirector, $urlEncode) {
122 $url = $urlEncode ? urlencode($matches[1]) : $matches[1];
123 return '<a href="' . $redirector . $url .'">'. $matches[1] .'</a>';
130 * Auto-link hashtags.
132 * @param string $description Given description.
133 * @param string $indexUrl Root URL.
135 * @return string Description with auto-linked hashtags.
137 function hashtag_autolink($description, $indexUrl = '')
140 * To support unicode: http://stackoverflow.com/a/35498078/1484919
141 * \p{Pc} - to match underscore
142 * \p{N} - numeric character in any script
143 * \p{L} - letter from any language
144 * \p{Mn} - any non marking space (accents, umlauts, etc)
146 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
147 $replacement = '$1<a href="'. $indexUrl .'?addtag=$2" title="Hashtag $2">#$2</a>';
148 return preg_replace($regex, $replacement, $description);
152 * This function inserts where relevant so that multiple spaces are properly displayed in HTML
153 * even in the absence of <pre> (This is used in description to keep text formatting).
155 * @param string $text input text.
157 * @return string formatted text.
159 function space2nbsp($text)
161 return preg_replace('/(^| ) /m', '$1 ', $text);
165 * Format Shaarli's description
167 * @param string $description shaare's description.
168 * @param string $redirector if a redirector is set, use it to gerenate links.
169 * @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not.
170 * @param string $indexUrl URL to Shaarli's index.
172 * @return string formatted description.
174 function format_description($description, $redirector = '', $urlEncode = true, $indexUrl = '') {
175 return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector, $urlEncode), $indexUrl)));
179 * Generate a small hash for a link.
181 * @param DateTime $date Link creation date.
182 * @param int $id Link ID.
184 * @return string the small hash generated from link data.
186 function link_small_hash($date, $id)
188 return smallHash($date->format(LinkDB
::LINK_DATE_FORMAT
) . $id);