Question

I'm trying to use strripos in fact I try to add a parameter to an URL fro translating my website,

the fact is that not over the url requested does have a parameter so I can not to everytime &lang=en but I need to do ?lang=en

I've tried to do a function using strripos like that

/**
 * Fonction qui renvoit le bon caractère en tant que paramètre
 * @param type $string
 * @return string
 */
function checkParam($string){
    $lookfor = "?";
    $position = strripos($string, $lookfor);
    if($position === true){
    return "&";
    }else{
    return "?";
    }
}

I have for example that url : $url_courante = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

which give me something like that

www.mydomain.eu/index.php?p=nos-produits

but it does not recognize the ? in that string, so it returns always to me false and I can not set properly my lang to the url.

Anykind of help will be much appreciated.

Was it helpful?

Solution

strripos returns an integer or false, not true. Switch the condition around and check for false.

function checkParam($string){
    $lookfor = "?";
    $position = strripos($string, $lookfor);
    if($position === false){
        return "?";
    }else{
        return "&";
    }
}

Also, there is no need for strripos because you are only checking to see if the string exists. It does not matter if you find the first or last. Case is not an issue when you are searching for a question mark, either. I would use strpos instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top