-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathHelper.php
48 lines (40 loc) · 982 Bytes
/
Helper.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
<?php
namespace Melbahja\Seo;
/**
* @package Melbahja\Seo
* @since v2.0
* @see https://git.io/phpseo
* @license MIT
* @copyright 2019-present Mohamed Elabhja
*/
class Helper
{
public static $encoding = 'UTF-8';
public static function escape(string $text): string
{
return htmlspecialchars($text, ENT_QUOTES | ENT_HTML5, static::$encoding);
}
/**
* Escape url for sitemaps.
*
* @param string $url
* @return string
*/
public static function escapeUrl(string $url): string
{
$url = parse_url($url);
$url['path'] = $url['path'] ?? '';
$url['query'] = $url['query'] ?? '';
if ($url['path'] !== '') {
$url['path'] = implode('/', array_map('rawurlencode', explode('/', $url['path'])));
}
if ($url['query'] !== '') {
$url['query'] = "?{$url['query']}";
}
return str_replace(
['&', "'", '"', '>', '<'],
['&', ''', '"', '>', '<'],
$url['scheme'] . "://{$url['host']}{$url['path']}{$url['query']}"
);
}
}