user14666
user14666

Reputation: 105

Comparing variable pathinfo

How do I get the path info (php) when the path is of the form: www.example.com/post/* (* being a variable number that matches the post id) and compare it to www.example.com/post to make it TRUE.

In other words, if pathinfo is www.example.com/post/1068 (or 1069, or 1070, or whatever the id number) then the statements that follow are executed. If not, then they are skipped.

The snippet is destined to a template file. If this requires a pre-process function, please say so. Thank you.

I am new at this so please do not send me to some other post that reads like Chinese! :)

Thank you.

Upvotes: 0

Views: 91

Answers (1)

taxicala
taxicala

Reputation: 21789

You could explode and filter for empty values and then use the last occurrence, something rough like this:

$url = "www.example.com/post/1068";
$id = array_pop(array_filter(explode('/', $url)));
$validIds = array("1068", "1069", "1070");
if (in_array($id, $validIds)) {
    //do something;
}

explode will separate your string in an array using "/" as a separator (in this case)

array_filter will filter empty values in order to make sure that the last value will be your id

array_pop will retreive the last value of the given array

in_array will compare a value with an array and check if the value is within the given array and return true/false depending whether it is or not inside the array.

Upvotes: 1

Related Questions