3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php // $base = 'test image with Ümlaäute und spaäe.jpg'; $nameEncodedFromGWT = 'test%20image%20with%20U%CC%88mlaa%CC%88ute%20und%20spaa%CC%88e.jpg'; $nameEncodedFromPHP = 'test%20image%20with%20%C3%9Cmla%C3%A4ute%20und%20spa%C3%A4e.jpg'; // this is encoded with urlencode() / rawurlencode() // using rawurldecode $nameDecodedFromGWTRaw = utf8_urldecode($nameEncodedFromGWT); $nameDecodedFromPHPRaw = rawurldecode($nameEncodedFromPHP); // using urldecode $nameDecodedFromGWTSimple = urldecode($nameEncodedFromGWT); $nameDecodedFromPHPSimple = urldecode($nameEncodedFromPHP); echo 'Name from GWT: ' . $nameEncodedFromGWT . PHP_EOL . 'Decoded using rawurldecode(): ' . $nameDecodedFromGWTRaw . PHP_EOL . 'Decoded using urldecode(): ' . $nameDecodedFromGWTSimple . PHP_EOL . 'String length for rawurldecode(): ' . strlen($nameDecodedFromGWTRaw) . PHP_EOL . 'String length for urldeocde(): ' . strlen($nameDecodedFromGWTSimple) . PHP_EOL . 'Cleaned name for rawurldecode(): ' . cleanImageName($nameDecodedFromGWTRaw) . PHP_EOL . 'Cleaned name for urldecode(): ' . cleanImageName($nameDecodedFromGWTSimple); echo PHP_EOL . '===============================================' . PHP_EOL; echo 'Name from PHP: ' . $nameEncodedFromPHP . PHP_EOL . 'Decoded using rawurldecode(): ' . $nameDecodedFromPHPRaw . PHP_EOL . 'Decoded using urldecode(): ' . $nameDecodedFromPHPSimple . PHP_EOL . 'String length for rawurldecode(): ' . strlen($nameDecodedFromPHPRaw) . PHP_EOL . 'String length for urldecode(): ' . strlen($nameDecodedFromPHPSimple) . PHP_EOL . 'Cleaned name for rawurldecode(): ' . cleanImageName($nameDecodedFromPHPRaw) . PHP_EOL . 'Cleaned name for urldecode(): ' . cleanImageName($nameDecodedFromPHPSimple); function utf8_urldecode($str) { $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str)); return html_entity_decode($str,null,'UTF-8');; } /** * Uses preg_replace to replace german umlauts */ function cleanImageName($name) { $clean = preg_replace( array( '/\|/', '/[äÄ]/u', '/[öÖ]/u', '/[üÜ]/u', '/ß/', '/([a-zA-Z0-9]+)/i', '/ - /','/ /','/-/','/&/', '/[á|à|À|Á]/u', '/[é|è|É|È]/u', '/[_\x7c-\xff\$\%\=\?\@\(\)§!\|`´\*#;,:\.<>\/"\'#\t\r\n]/' ), array( ' ', 'ae', 'oe', 'ue', 'ss', '$1', ' ', ' ', ' ', 'und', 'a', 'e', ' ', ), $name); return str_replace(' ', '-', $clean); } ?>
based on sLYTR
Output for 5.3.14 - 7.2.0
Name from GWT: test%20image%20with%20U%CC%88mlaa%CC%88ute%20und%20spaa%CC%88e.jpg Decoded using rawurldecode(): test image with Ümlaäute und spaäe.jpg Decoded using urldecode(): test image with Ümlaäute und spaäe.jpg String length for rawurldecode(): 44 String length for urldeocde(): 44 Cleaned name for rawurldecode(): test-image-with-U--mlaa--ute-und-spaa--e-jpg Cleaned name for urldecode(): test-image-with-U--mlaa--ute-und-spaa--e-jpg =============================================== Name from PHP: test%20image%20with%20%C3%9Cmla%C3%A4ute%20und%20spa%C3%A4e.jpg Decoded using rawurldecode(): test image with Ümlaäute und spaäe.jpg Decoded using urldecode(): test image with Ümlaäute und spaäe.jpg String length for rawurldecode(): 41 String length for urldecode(): 41 Cleaned name for rawurldecode(): test-image-with-uemlaaeute-und-spaaee-jpg Cleaned name for urldecode(): test-image-with-uemlaaeute-und-spaaee-jpg