php if elseif wildcards based on URLs

689 views Asked by At

Searched high and low for an easier way to do what I do...

I have a function that produces $absLink result as 'www.example.com/current/page/'

I then use this for php if and elseif to do things like:

<?php else if ($absLink == 'www.example.com/homepage/' { ?>
This the homepage
<?php } elseif ($absLink == 'www.example.com/about/' { ?>
This the about page
<?php } else { ?>
This probably the 404 not found page
<?php } ?>

But sometimes URLs get really deep and I'd like to include similar css styles for multiple pages. Let's say /about/ also has /about/staff and about/mission-statement/ and in the future more pages will be added... Can I do a wild card? In my mind it would work like this:

<?php if ($absLink == 'www.ohiowebgroup.com/about/*') { ?>
Everything in the about directory follows suit to everything here
<?php } else { ?>
Rejection happens here lol
<?php } ?>

Make sense? p.s. $absLink stands for Absolute Link, although I removed http:// from it recently cause that's just a non-useful annoyance I decided.

1

There are 1 answers

2
Venkata Krishna On

You can see if the variables contains the url using strpos.

if (strpos($absLink,'www.ohiowebgroup.com/about/')>=0)

So here,
if $absLink contains www.ohiowebgroup.com/about/, it will returns its position which will be zero if found in the beginning or a positive number based on where it is found. Now this condition will be true irrespective of what comes after about/, so it works good for your problem.

Hope this helped.