3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $str = <<<EOD http://localhost:8000/search/fa?q=sth http://localhost:8000/search?q=sth http://localhost:8000/search http://localhost:8000/search/fa EOD; preg_match_all('/^(.*?)\/(\w{2,})(?:\?(.*?)|)$/mis',$str,$p); echo '<pre/>'; $final_array = array(); $i = 0; foreach($p[1] as $myP){ if( $p[2][$i] == 'fa'){ $p[2][$i] = 'en'; } else if($p[2][$i] != 'en') { $p[2][$i] = $p[2][$i].'/'.'en'; } $final_array[$i] = $myP.'/'.$p[2][$i]; $i++; } var_dump($final_array);
based on u9hoM
Output for 5.6.0 - 7.3.0alpha1
<pre/>array(4) { [0]=> string(31) "http://localhost:8000/search/en" [1]=> string(32) " http://localhost:8000/search/en" [2]=> string(32) " http://localhost:8000/search/en" [3]=> string(32) " http://localhost:8000/search/en" }