1

I use Codeigniter to create a multilingual website and everything works fine, but when I try to use the "alternative languages helper" by Luis I've got a problem. This helper uses a regular expression to replace the current language with the new one:

$new_uri = preg_replace('/^'.$actual_lang.'/', $lang, $uri);

The problem is that I have a URL like this: http://www.example.com/en/language/english/ and I want to replace only the first "en" without changing the word "english". I tried to use the limit for preg_replace:

$new_uri = preg_replace('/^'.$actual_lang.'/', $lang, $uri, 1);

but this doesn't work for me. Any ideas?

0

2 Answers 2

1

You could do something like this:

$regex = '#^'.preg_quote($actual_lang, '#').'(?=/|$)#';
$new_uri = preg_replace($regex, $lang, $uri);

The last capture pattern basically means "only match if the next character is a forward slash or the end of the string"...

Edit:

If the code you always want to replace is at the beginning of the path, you could always do:

if (stripos($url, $actual_lang) !== false) {
    if (strpos($url, '://') !== false) {
        $path = parse_url($url, PHP_URL_PATH);
    } else {
        $path = $url;
    }
    list($language, $rest) = explode('/', $path, 2);
    if ($language == $actual_lang) {
        $url = str_replace($path, $lang . '/' . $rest, $url);
    }
}

It's a bit more code, but it should be fairly robust. You could always build a class to do this for you (by parsing, replacing and then rebuilding the URL)...

Sign up to request clarification or add additional context in comments.

3 Comments

The URL given in the question will still be matched by that regex.
It would still be matched, but only the first en part. The trailing english/ would not be matched (and hence would not be replaced)...
Actually, I spoke too soon. The URL won't be matched at all, because the URL doesn't start with en. Without the ^ anchor, however, http://www.example.com/en/language/en/ would match twice.
1

If you know what the beginning of the URL will always, be, use it in the regex!

$domain = "http://www.example.com/"
$regex = '#(?<=^' . preg_quote($domain, '#') . ')' . preg_quote($actual_lang, '#') . '\b#';
$new_uri = preg_replace($regex, $lang, $uri);

In the case of your example, the regular expression would become #(?<=^http://www.example.com/)en\b which would match en only if it followed the specified beginning of a domain ((?<=...) in a regular expression specifies a positive lookbehind) and is followed by a word boundary (so english wouldn't match).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.