note 128862 added to function.idn-to-ascii

From: Date: Wed, 06 Sep 2023 09:40:54 +0000
Subject: note 128862 added to function.idn-to-ascii
Groups: php.notes 
Request: Send a blank email to php-notes+get-220823@lists.php.net to get a copy of this message
idn_to_ascii and idn_to_utf8 functions don't properly handle full URLs (i.e. with schema and paths), so here's the helper functions which handles all URLs, including ones with path but without a scheme <?php /** * Converts URLS to punycode * It doesn't url-encodes other parts * The initial code from snipp dor ru website, here is modified version that handles urls without scheme */ function punycode_encode($url) { $no_scheme = false; if (!preg_match('/^.+?:\/\//', $url) && substr($url, 0, 2) !== '//') { $url = '//' . $url; $no_scheme = true; } $parts = parse_url($url); $out = ''; if (!empty($parts['scheme'])) $out .= $parts['scheme'] . ':'; if (!empty($parts['host'])) $out .= '//'; if (!empty($parts['user'])) $out .= $parts['user']; if (!empty($parts['pass'])) $out .= ':' . $parts['pass']; if (!empty($parts['user'])) $out .= '@'; if (!empty($parts['host'])) $out .= idn_to_ascii($parts['host']); if (!empty($parts['port'])) $out .= ':' . $parts['port']; if (!empty($parts['path'])) $out .= $parts['path']; if (!empty($parts['query'])) $out .= '?' . $parts['query']; if (!empty($parts['fragment'])) $out .= '#' . $parts['fragment']; if ($no_scheme) { $out = substr($out, 2); } return $out; } function punycode_decode($url) { $no_scheme = false; if (!preg_match('/^.+?:\/\//', $url) && substr($url, 0, 2) !== '//') { $url = '//' . $url; $no_scheme = true; } $parts = parse_url($url); $out = ''; if (!empty($parts['scheme'])) $out .= $parts['scheme'] . ':'; if (!empty($parts['host'])) $out .= '//'; if (!empty($parts['user'])) $out .= $parts['user']; if (!empty($parts['pass'])) $out .= ':' . $parts['pass']; if (!empty($parts['user'])) $out .= '@'; if (!empty($parts['host'])) $out .= idn_to_utf8($parts['host']); if (!empty($parts['port'])) $out .= ':' . $parts['port']; if (!empty($parts['path'])) $out .= $parts['path']; if (!empty($parts['query'])) $out .= '?' . $parts['query']; if (!empty($parts['fragment'])) $out .= '#' . $parts['fragment']; if ($no_scheme) { $out = substr($out, 2); } return $out; } ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 185.21.142.210) ---- Manual Page -- https://php.net/manual/en/function.idn-to-ascii.php Edit -- https://main.php.net/note/edit/128862 Del: integrated -- https://main.php.net/note/delete/128862/integrated Del: useless -- https://main.php.net/note/delete/128862/useless Del: bad code -- https://main.php.net/note/delete/128862/bad+code Del: spam -- https://main.php.net/note/delete/128862/spam Del: non-english -- https://main.php.net/note/delete/128862/non-english Del: in docs -- https://main.php.net/note/delete/128862/in+docs Del: other reasons-- https://main.php.net/note/delete/128862 Reject -- https://main.php.net/note/reject/128862 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220823) next »