Re: [Tiki-devel] Function for encoding anchor names
Volker Wysk <post-hhF2Jplw28UoZk/[email protected]>
| Newsgroups | gmane.comp.cms.tiki.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi. Here it is again. I've renamed the function and added some comments. Volker Am Freitag, dem 24.02.2023 um 06:15 +0100 schrieb Volker Wysk: > Hi! > > I've written a PHP function which does a standards-compliant encoding of an > anchor name to a URL fragment string (the part of an URL after the "#"). > > Now this function needs to be used in Tiki. > > See attachment. > > Volker > _______________________________________________ > TikiWiki-devel mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/tikiwiki-devel _______________________________________________ TikiWiki-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/tikiwiki-devel
anchor_encode.php
(application/x-php, 2.5 KB)
<?php
/* This function encodes an anchor name as a so-called "URL-fragment string". That's the part of an URL after the
* "#" (if any). Except for spaces, which are encoded as hyphens, not as "%20". See here in the URL standard for
* the formal definition: https://url.spec.whatwg.org/#url-fragment-string
*/
function anchor_encode($txt)
{
$arr = mb_str_split($txt);
$frag = "";
for ($i = 0; $i < count($arr); $i++) {
$chr = $arr[$i];
$ord = IntlChar::ord($chr);
if ($chr == " ") {
// Encode spaces by hyphens.
$frag .= "-";
} else {
if ( // URL code points
(ctype_alnum($chr)
|| $chr == "!" || $chr == "$" || $chr == "&" || $chr == "'" || $chr == "(" || $chr == ")"
|| $chr == "*" || $chr == "+" || $chr == "," || $chr == "-" || $chr == "." || $chr == "/"
|| $chr == ":" || $chr == ";" || $chr == "=" || $chr == "?" || $chr == "@" || $chr == "_"
|| $chr == "~"
|| ($ord >= 0x00A0 && $ord <= 0x10FFFD))
// surrogates
&& !($ord >= 0xD800 && $ord <= 0xDFFF)
// noncharacters
&& !( ($ord >= 0xFDD0 && $ord <= 0xFDEF)
|| $ord == 0xFFFE || $ord == 0xFFFF || $ord == 0x1FFFE || $ord == 0x1FFFF
|| $ord == 0x2FFFE || $ord == 0x2FFFF || $ord == 0x3FFFE || $ord == 0x3FFFF
|| $ord == 0x4FFFE || $ord == 0x4FFFF || $ord == 0x5FFFE || $ord == 0x5FFFF
|| $ord == 0x6FFFE || $ord == 0x6FFFF || $ord == 0x7FFFE || $ord == 0x7FFFF
|| $ord == 0x8FFFE || $ord == 0x8FFFF || $ord == 0x9FFFE || $ord == 0x9FFFF
|| $ord == 0xAFFFE || $ord == 0xAFFFF || $ord == 0xBFFFE || $ord == 0xBFFFF
|| $ord == 0xCFFFE || $ord == 0xCFFFF || $ord == 0xDFFFE || $ord == 0xDFFFF
|| $ord == 0xEFFFE || $ord == 0xEFFFF || $ord == 0xFFFFE || $ord == 0xFFFFF
|| $ord == 0x10FFFE || $ord == 0x10FFFF)
)
{
// Allowed character.
$frag .= $chr;
} else {
// Unallowed character - must be percent-encoded.
$bytes = str_split($chr);
for ($j = 0; $j < count($bytes); $j++) {
$frag .= "%" . bin2hex($bytes[$j]);
}
}
}
}
return $frag;
}
// Test: echo anchor_encode("abc/关一条!!äöü 龙服务 Tiki ist gut.");
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmP4UYEACgkQ+K4ydFOs HoUsIA/+NePf+vcAUVurfCoNun0JSsVv1xxJOKboKwOtrsBBmp6HjNzYt/SH1gFq UfstNB1D8hQHaHqr/OVJ4ZxxrBmFJ0DgQK1rlL9KCWoegy85l16iTH1oh3dd4PLX mEeh/JtlClkDYF0dAzn0ppALaaf/pR/gJZ86oSIZ3oDk0/n27iBKTfSMtwRlABoD /7P4I6OlCqexGpDHqaNsJgIPHn+rfXFMapb/iWIkprDfsLYR4dKfKx75GsdxyC9Q OQQ/oLhcVVUAEM4GLSMKNQIp8mlbxW0RM326/biHTNJqa+zNz8v3wl1qMweBrJSb oC9iQhCXHH2EzebzWqxppp1pYuQyVhR8X36KV5dzkLOn15IEAkhw5I7U66eNCB/V D24U+4hxsI2jC30hU/dk+o3cclSPclqr4ALD5NGz/cqiEnd28nDjzCPoid8a3xE9 PCk/0+c118rm/tA95XhPPD2f+NHvwhrcsi6OblA95fhCa7tpPq1Yn7nDQxgCPeVk S06iGIuIGq0fX8Eb7O7hg9D28zfYb5oFFSUVhHGLxvgOM3bWzS45MoMacvOi6YcR YJK88ZRDSJ7j2Grs5MtVshkih9kQZl1NFJ8vnq+Q+ZhJIxfuP8w55KtKAzJm7qdo M3q3t4S4SK12/4AHY/R3pX9g666Li7D1QcEkpwJaZruGGlvFaZw= =8LGE -----END PGP SIGNATURE-----