Insert a css file in a URL level

Good morning.

I have a website with a series of categories and subcategories.

I have been using this script to insert css files for specific urls:


<?php
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri,'url-address')  !== false ) {
   echo '<link href="/css/the-file.css" rel="stylesheet" type="text/css" />';
}
?>

Now I need to include a css file in a really large number of pages displayed in a directory. All of them in the fourth level after the domain:

www. the site com/level1/level2/level3/this-is-one-page

If only one page, no problem:


if (strpos($uri,'level1/level2/level3/this-is-one-page')  !== false ) {
   echo '<link href="/css/the-file.css" rel="stylesheet" type="text/css" />';
}

But imagine hundreds of them.

I am thinking of some script to include the-file.css in all the pages in the fourth level, with not to much luck.

Some idea?

If you want the css in all the pages in that directory, why can’t you just:


if (strpos($uri, 'level1/level2/level3/') != false) {

Or might you have another level of directory below that, which would not require that CSS file to be included? If so, you could probably do some work with checking whether there’s another directory marker in the remaining string.

Hi droopsnoot, thanks for replying.

I didn’t describe the issue properly.

We are talking about a directory with seven first-level options, more than hundred secondary levels, several hundreds of third-level categories and finally the fourth level, where I need to add the css styling.

So imagine the combinations in the structure.

“level1/level2/level3/” are just a figurative way of describing it, which such levels filled by different category and subcategory names.

The idea is that wherever you are in the directory, the fourth-level pages receive an special style from a single css file.

Ah, OK. If it’s just a case of being in a fourth-level directory, regardless of what fourth-level directory it is, how about using explode() on the uri based on the forward-slash, then see how many entries it splits into? If you end up with an array containing four elements, you’re in the right level. Or use strpos() to count slashes in the uri, for much the same result.

Yes.

I have been trying to implement the second solution, using strpos() on the information in http://php.net/manual/en/function.strpos.php, but I cannot complete a correct function.

You know some practical example of counting elements in URI using strpos()?

Not for sure, but wouldn’t this do it?


function countslashes($s) {
$a = 0;
$start = 0;
while ($x = strpos($s, '/', $start)) {
  $a += 1;
  $start = $x+1;
  }
return $a;
}

I haven’t tested it, but it seems reasonable - start at position zero, look for a slash, when you find one increment the counter, then search again starting from the next character after that slash. You might need to mess around with the starting position, I know it starts at zero but I couldn’t quite see whether it would return the base-zero position or the base-one position.

Or:


$a = explode('/', $uri);
$x = count($a);

That should return the number of elements of the uri in $x, splitting on the forward-slash.

There is actually a built in function for this: [fphp]substr_count[/fphp]


if (substr_count($_SERVER['REQUEST_URI'], '/') === 4)
{
    // include my css here
}

Your two solutions work great for me.

Tested and solved.

Thanks very much.