I am trying to take the base URL from a site and be able to print the first and possibly second directory in the path. The code I have tested below works for anything with more than 2 directories (eg. dir/dir/title). The problem is when there is only 1 or two directories in the path (eg dir/title or just title).
<?php
$path = "dir/dir/dir/title";
$posl = substr($path, 0, strpos($path, "/"));
$post_strip = $posl."/";
$new_path = str_replace($post_strip, "", $path);
$new_path = substr($new_path, 0, strpos($new_path, "/"));
echo $new_path;
?>
I was also thinking there might be some way to split the path string at the slashes and hold each of these as an array. Therefore I can just print the specific value of the array as it corresponds to its place in the path.
Any help is greatly appreciated.