<?php
$strs = [
'/' => true,
'/?foo=bar' => true,
'/?' => true,
'/en' => true,
'/ru' => true,
'/ru/foo' => false
];
$expr = '/^\/(en|ru)?(\?.*)?$/';
$success = true;
foreach ($strs as $str=>$expected) {
$success = $success && $expected == preg_match($expr, $str); }
echo $success ?
'Tests passed!' : 'Tests failed!';
PD9waHAKCiRzdHJzID0gWwoJJy8nID0+IHRydWUsCgknLz9mb289YmFyJyA9PiB0cnVlLAoJJy8/JyA9PiB0cnVlLAoJJy9lbicgPT4gdHJ1ZSwKCScvcnUnID0+IHRydWUsCgknL3J1L2ZvbycgPT4gZmFsc2UKXTsKCiRleHByID0gJy9eXC8oZW58cnUpPyhcPy4qKT8kLyc7Cgokc3VjY2VzcyA9IHRydWU7CmZvcmVhY2ggKCRzdHJzIGFzICRzdHI9PiRleHBlY3RlZCkgewoJJHN1Y2Nlc3MgPSAkc3VjY2VzcyAmJiAkZXhwZWN0ZWQgPT0gcHJlZ19tYXRjaCgkZXhwciwgJHN0cik7Cn0KCmVjaG8gJHN1Y2Nlc3MgPyAKICAgJ1Rlc3RzIHBhc3NlZCEnIDogJ1Rlc3RzIGZhaWxlZCEnOw==