Welcome Guest, Not a member yet? Register   Sign In
PHP strpos
#1

[eluser]frist44[/eluser]
I'm trying to test if the string 'login' or 'join' is in my URL and if so, do set a session variable.

For just one, I have:

if (strpos(cur_url(), 'login') === FALSE) $this->set_prev_url();

however, if I add an "or" condition to put 'join', it'll write the session even when it's on the login, because the 'join' condition will be satisfied. How could I pass in a few strings and test the same condition?
#2

[eluser]BrianDHall[/eluser]
I'm not sure exactly what you are asking, but do you need something like this?

Code:
if ( strpos(cur_url(), ‘login’) === FALSE OR (strpos(cur_url(), ‘login’) === FALSE AND strpos(cur_url(), ‘join’) !== FALSE) )
#3

[eluser]frist44[/eluser]
I guess i have a few strings that I know need to be tested. If any of them are in the URL, i do NOT want the function to run.

So if (strpos(cur_url(), ‘login’) === FALSE) run the function.

I (strpos(cur_url(), ‘login’) === FALSE) or (strpos(cur_url(), ‘join’) === FALSE) run the function.

however, the above will test each. So if I am on 'login', the (strpos(cur_url(), ‘join’) === FALSE) will be matchen and the function will run even thought i'm on 'login'.

So ideally, i would like to have a list/array of strings, and if any of them are present in cur_url(), do NOT run the function.

It seems to obvious in your head sometimes, but much harder to explain clearly in words.

Thanks!
#4

[eluser]BrianDHall[/eluser]
OOO, you just need AND - require that every test be false.

You can wrap it into a function, something like:

Code:
function check_url($strings)
{

if (! isarray($strings))
{
$strings[] = $strings;
}

foreach ($strings as $str)
{
if (strpos(cur_url(), $str) !== FALSE)
{
return false;
}
}

return true;
}

Then you can just pass one string or an array of strings and the function will return false if any of the strings are in the url, and true if it is clean of all unwanted items.
#5

[eluser]frist44[/eluser]
Yeah, you're right. I don't know why I didn't think of that. I ended up with this solution:

Code:
if (!is_array_in_string($this->_non_record_url, cur_url())){
           $this->set_prev_url();
       }

with this helper:

Code:
function is_array_in_string($xArr, $xStr) {        
        if (!is_array($xArr)) $xArr[] = $xArr;        
        foreach ($xArr as $el) if (strpos($xStr, $el) !== FALSE) return TRUE;
        return FALSE;
    }




Theme © iAndrew 2016 - Forum software by © MyBB