3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class TranslationError extends \Exception { public function __construct(string $message){ parent::__construct($message); } } class TranslationUtils { /** * List of strings that are required to be in the translation if they appear in the english one */ private const MANDATORY_MATCHES = ['/…/', '/%(\\d+\$)?(b|B|h|H|s|S|c|C|d|i|x|X|e|E|f|g|G|a|A|t|T|n)/']; public static function checkTranslation(string $original, string $translation){ //CHECK MANDATORY STRINGS foreach(static::MANDATORY_MATCHES as $pattern){ $matches = []; if(preg_match_all($pattern, $original, $matches) >= 1){ foreach($matches[0] as $match){ if(strpos($translation, $match) === FALSE){ throw new TranslationError("Missing '" . $match . "' from translation!"); } } } } } public static function isTranslationOk(string $original, string $translation) : bool { try { TranslationUtils::checkTranslation("Loading…", "adsda..."); return true; } catch(TranslationError $ex){ var_dump($ex->getMessage()); return false; } } } var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda...")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda…")); var_dump(TranslationUtils::isTranslationOk("Ciao %s", "Ciao %s")); var_dump(TranslationUtils::isTranslationOk("NOPE %d", "YES %s")); var_dump(TranslationUtils::isTranslationOk("Allo %\$1s", "Hello %\$1s %\$2s")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda...")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda...")); var_dump(TranslationUtils::isTranslationOk("Loading…", "adsda..."));
based on sWlhl
Output for 7.1.0 - 7.3.0alpha1
string(31) "Missing '…' from translation!" bool(false) string(31) "Missing '…' from translation!" bool(false) string(31) "Missing '…' from translation!" bool(false) string(31) "Missing '…' from translation!" bool(false) string(31) "Missing '…' from translation!" bool(false) string(31) "Missing '…' from translation!" bool(false) string(31) "Missing '…' from translation!" bool(false) string(31) "Missing '…' from translation!" bool(false)
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.0.20
Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in /in/urMVf on line 16
Process exited with code 255.
Output for hhvm-3.15.4
Fatal error: Uncaught Error: syntax error, unexpected T_CONST, expecting T_VARIABLE in /in/urMVf:16 Stack trace: #0 {main}
Process exited with code 255.