3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = 'Showing results [program=for {program} programs|No pogram at all] [province=in {province}] [degree=with degree of {degree}]'; function interpolateString($message, array $context = array()) { $arrMatches = array(); preg_match_all("/\[(\w+)\=([^\[\]\|.*]*)?\|*([^\[\].*]*)\]/", $str, $arrMatches); $contextKeys = $arrMatches[1]; $contextCondition = array(); $contextValues = array(); $i = 0; foreach ($contextKeys as $key){ $contextCondition += array($key => $arrMatches[0][$i]); $i++;} $i = 0; foreach ($contextKeys as $key){ $value = $arrContext[$key] != false ? 2 : 3; $contextValues += array($key => $arrMatches[$value][$i]); $i++;} $str = $message; foreach ($contextKeys as $key){ $str = str_replace($contextCondition[$key], $contextValues[$key], $str); } $message = $str; $replace = array(); foreach ($context as $key => $val) { $replace['{' . $key . '}'] = $val; } return strtr($message, $replace); } $arrContext = array( 'program' => 'Nursing', 'province' => 'Ontario', 'degree' => false ); echo interpolateString($str, $arrContext); echo "\n";
Output for git.master, git.master_jit, rfc.property-hooks
Warning: Undefined variable $str in /in/V3ETT on line 8 Deprecated: preg_match_all(): Passing null to parameter #2 ($subject) of type string is deprecated in /in/V3ETT on line 8 Showing results [program=for Nursing programs|No pogram at all] [province=in Ontario] [degree=with degree of ]

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
37.97 ms | 401 KiB | 8 Q