-3

Confused! I am trying to extract our localsites' names which are embedded in our WordPress website's structure, ie https://www.website.co.uk/site99/folderX/page. In the URL the localsite name is all lower case without any gaps, whereas the 'display name' of the localsite has a Starting capital and a gap between words. What am I doing wrong here?

if(strpos($_SERVER['REQUEST_URI'], '/site01/') !== false) { $categoryIdOrSlug = 'Site 01';}
elseif (strpos($_SERVER['REQUEST_URI'], '/site02/') !== false) { $categoryIdOrSlug = 'Site 02';}
elseif (strpos($_SERVER['REQUEST_URI'], '/site03/') !== false) { $categoryIdOrSlug = 'Site 03';}
endif;

so what I am looking for is where /site99/ is in position 0 of the slug.

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • So what exactly is the problem/question? – JJJ Apr 03 '18 at 07:19
  • 2
    Please check the documentation before asking something here. It is [clearly documented](https://secure.php.net/manual/en/function.strpos.php#example-5844) why we should be using `!==` instead of `!=`. If this is not what you are asking, please update your question to reflect what you are actually asking us. – Tom Udding Apr 03 '18 at 07:24
  • Hi JJJ, the problem is that $categoryIdOrSlug is not being populated with the site description, ie when the url is www.website.co.uk/site01/pageA, $categoryIdOrSlug is not being populated with 'Site 01' or when www.website.co.uk/site02/pageA with 'Site 02'. – Christopher Hilling Apr 03 '18 at 16:21
  • Hi Tom, yes I know !== and != is documented and I believe the above code snippet is the correct use of !==, if I didn't I would not be asking the question. – Christopher Hilling Apr 03 '18 at 16:23

1 Answers1

-1

If you are in https://www.website.co.uk/site99/folderX/page

Then : $_SERVER['REQUEST_URI'] will be /site99/folderX/page

if (strpos($_SERVER['REQUEST_URI'], '/site98/') !== false) { $categoryIdOrSlug = 'Site 98';} // strpos($_SERVER['REQUEST_URI'], '/site98/') will be false
elseif (strpos($_SERVER['REQUEST_URI'], '/site99/') !== false) { $categoryIdOrSlug = 'Site 99';} // strpos($_SERVER['REQUEST_URI'], '/site98/') will be 0 (which is success run & is true)

See different outputs:

echo strpos($_SERVER['REQUEST_URI'], '/site99/'); // OUTPUT: 0

Here, strpos() return the index/position at which the 2nd param(/site99/) is present in first param($_SERVER['REQUEST_URI']).

The strpos() function finds the position of the first occurrence of a string inside another string. Related functions: strrpos() - Finds the position of the last occurrence of a string inside another string (case-sensitive)

var_dump(strpos($_SERVER['REQUEST_URI'], '/site98/'));

Output will be bool(false). Why its false because the string was not found in param one. If its found, then the out will be a int of position like int(25).

var_dump(strpos($_SERVER['REQUEST_URI'], '/site99/'));

Output will be int(0) because strpos() has successfully found /site99/ in $_SERVER['REQUEST_URI'].

And, normally === or !== are used for Boolean value(TRUE/FALSE) comparisons.

No need to get confused with strpos() and !== both are entirely different, one returns a position index value(if found) or FALSE, if not found required string. Whereas, !== compares left & right side values of operator & says TRUE or FALSE.

Reply to your comment:

Yes, you are getting correct answer. As you said to making ensure we are using if() check.

You see we are getting int(0) while var_dump(strpos($_SERVER['REQUEST_URI'], '/site99/'));. If we have a index value(even its 0) its treated as a success run(true). That is, the string you are searching is found in index 0 or at a particular position of $_SERVER['REQUEST_URI']. if() makes sure that the check is correct, int(0) !== false means int(0) is a success run(which is also said as true). So if(int(0) !== false) can be said also as if(true !== false) which is true & so $categoryIdOrSlug = 'Site 99'; will run.

if() can also be written as:

if (strpos($_SERVER['REQUEST_URI'], '/site98/')) { $categoryIdOrSlug = 'Site 98';}
elseif (strpos($_SERVER['REQUEST_URI'], '/site99/')) { $categoryIdOrSlug = 'Site 99';}

echo $categoryIdOrSlug; // output: Site 99

You cant check by === 0 because we cant say the position at which strpos() founds the searching string, it may vary.

Sinto
  • 3,915
  • 11
  • 36
  • 70
  • Does $_SERVER['REQUEST_URI'] not returned the URL after the domain? ie '/site99/folderX/page' the relative path from the root Reading your answer above my code should work as strpos($_SERVER['REQUEST_URI'], '/site99/') should return int(0). To ensure I get the right answer, I need to use !== FALSE ? If I used === 0, the 'if' would resolve to TRUE if the 2nd param was '/site99/' as it would be position int(0) AND if the 2nd param was '/site98/', as the text is not present it return 0/false which would also resolve to TRUE when tested by === 0. Correct or have I missed something? – Christopher Hilling Apr 03 '18 at 16:52
  • @ChristopherHilling: I have edited my answer & commented on `if()` please read those. – Sinto Apr 04 '18 at 05:36