function urlencodeMultibyteCharacters($string) {
    $result = '';

    for ($i = 0; $i < mb_strlen($string, 'UTF-8'); $i++) {
        $char = mb_substr($string, $i, 1, 'UTF-8');
        $encodedChar = urlencode($char);
        $result .= ($char === $encodedChar) ? $char : $encodedChar;

 		$result = str_replace(' ', '%2F', $result);
 		$result = str_replace('-', '%2D', $result);
		$result = str_replace('&', '%26', $result);
    }


    return $result;
}

$string = "This is a sample string with some multibyte characters: こんにちは";
$encodedString = urlencodeMultibyteCharacters($string);

echo $encodedString;


  • No labels