PHP - change first folder in url but keep remaining folders as is -


using if else statement, php check if first folder in url (en), example: http://www.domain.com/en/newyork/car/ if first folder [ en ] shown above in example, change ahref of link have on page

change link from: en

change link to: ru

looking @ example above, thing want php change en or ru keeps remaining url of current page is

: if current page url http://www.domain.com/en/newyork/car/ link russian

in order current language of page, first have path of current url, , string after first slash. follows:

// current url $current_url = $_server['request_uri'];  // 'path' portion (without things 'http') $url = parse_url($current_url);  // split string in array $parts = explode('/', $url['path']); $lang = $parts[1]; $prepath = $url['scheme'] . '://' . $url['host'] . $parts[0];  // array slice remaining parts $postpath = array_slice($parts, 2);  // append first part of path, new language, ,  // remainder of url. $newurl = $prepath . '/' . ($lang == 'ru'?'':'ru/') . implode('/', $postpath); 

after this, can use anchor on page allow user change:

<a href="<?php echo $newurl;?>">change language</a> 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -