Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to replace a language identifier in a URL?
    primarykey
    data
    text
    <p>I need some help I have written the following preg_replace. The idea of the script is to get the language name from the domain name:</p> <p>If I have a </p> <pre><code>http://www.domainname.com/nl/this/is/a/test/index.asp </code></pre> <p>I would like to strip the <code>(nl)</code> part from the domain name. The nl is a variable for languages so it could be:</p> <ul> <li><a href="http://www.domainname.com/nl/this/is/a/test/index.asp" rel="nofollow">http://www.domainname.com/nl/this/is/a/test/index.asp</a></li> <li><a href="http://www.domainname.com/fr/this/is/a/test/index.asp" rel="nofollow">http://www.domainname.com/fr/this/is/a/test/index.asp</a></li> <li><a href="http://www.domainname.com/de/this/is/a/test/index.asp" rel="nofollow">http://www.domainname.com/de/this/is/a/test/index.asp</a></li> </ul> <p>The above domains represents language directories...</p> <pre><code>&lt;?php $sitename = "http://" .$_SERVER["SERVER_NAME"]; $pagename = $_SERVER["PHP_SELF"]."this/is/a/test"; $language = "\/..\/"; $language1 = preg_replace("/$language/", "$1", "$pagename"); ?&gt; </code></pre> <ul> <li>I am only using the script where I know there will be a language directory.</li> <li>The script above also removes the this/a/test/ the (is) from the url. (which tells me the script is greedy.</li> </ul> <p>What would be the best method to grab the language from the url?</p> <p>Can someone please provide me with some guidence.</p> <p>Thanks</p> <p>This is the script I came up using the advice below:</p> <pre><code>&lt;?php $mainurl = "http://" .$_SERVER["SERVER_NAME"]; $fullpagename = $_SERVER["PHP_SELF"]; $mainlanguage = preg_replace('%^/(\w+?)/.*$%', '$1', $_SERVER["REQUEST_URI"]); $strippedpagename = preg_replace("/$mainlanguage\//", '$1', $_SERVER["REQUEST_URI"]); ?&gt; &lt;?php echo "Full URL: ".$mainurl ?&gt;&lt;br&gt; &lt;?php echo "Full Page Name: ".$fullpagename ?&gt;&lt;br&gt; &lt;?php echo "Language: ".$mainlanguage ?&gt;&lt;br&gt; &lt;?php echo "Page Name No Language: ".$strippedpagename ?&gt;&lt;br&gt; </code></pre> <p>Exactly what I needed, or is there a more elegant way?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload