Iri::to_uri( string|bool $iri ): string|false

In this article

Convert an IRI to a URI (or parts thereof)

Parameters

$iristring|boolrequired
IRI to convert (or false from WpOrgRequestsIri::get_iri())

Return

string|false URI if IRI is valid, false otherwise.

Source

protected function to_uri($iri) {
	if (!is_string($iri)) {
		return false;
	}

	static $non_ascii;
	if (!$non_ascii) {
		$non_ascii = implode('', range("\x80", "\xFF"));
	}

	$position = 0;
	$strlen = strlen($iri);
	while (($position += strcspn($iri, $non_ascii, $position)) < $strlen) {
		$iri = substr_replace($iri, sprintf('%%%02X', ord($iri[$position])), $position, 1);
		$position += 3;
		$strlen += 2;
	}

	return $iri;
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.