• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP strpos

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?

I'm not sure exactly what you are asking, but do you need something like this?

if ( strpos(cur_url(), ‘login’) === FALSE OR (strpos(cur_url(), ‘login’) === FALSE AND strpos(cur_url(), ‘join’) !== FALSE) )

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.


OOO, you just need AND - require that every test be false.

You can wrap it into a function, something like:

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.

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

if (!is_array_in_string($this->_non_record_url, cur_url())){

with this helper:

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;

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.