0

I have a string which can be written in a number of different ways, it will always follow the same pattern but the length of it can differ.

this/is/the/path/to/my/fileA.php
this/could/also/be/the/path/to/my/fileB.php
another/example/of/a/long/address/which/is/a/path/to/my/fileC.php

What I am trying to do is cut the string so that I am left with

path/to/my/file.php

I have some code which I got from this page and modified it to the following

$max = strlen($full_path);
$n = 0;
for($j=0;$j<$max;$j++){
    if($full_path[$j]=='/'){
        $n++;
        if($n>=3){
            break 1;
        }
    }
}
$path = substr($full_path,$j+1,$max);

Which basically cuts it at the 3rd instance of the '/' character, and gives me what is left. This was fine when I was working in one environment, but when I migrated it to a different server, the path would be longer, and so the cut would give me too long an address. I thought that rather than changing the hard coded integer value for each instance, it would work better if I had it cut the string at the 4th from last instance, as I always want to keep the last 4 'slashes' of information.

EDIT - final code solution

$exploded_name = explode('/', $full_path);
$exploded_trimmed = array_slice($exploded_name, -4);
$imploded_name = implode('/', $exploded_trimmed);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Andrew Morris
  • 1,602
  • 3
  • 14
  • 26

3 Answers3

1

just use explode with your string and if pattern is always the same then get last element of the array and your work is done

$pizza  = "piece1/piece2/piece3/piece4/piece5/piece6";
$pieces = explode("/", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

Then reverse your array get first four elements of array and combine them using "implode" to get desired string

lostTiger
  • 82
  • 2
  • 4
0

This function below can work like a substr start from nth occurrence

function substr_after_nth($str, $needle, $key) 
{
    $array = explode($needle, $str);
    $temp = array();
    for ($i = $key; $i < count($array); $i++) {
        $temp[] = $array[$i];
    }
    return implode($needle, $temp);
}

Example

$str = "hello-world-how-are-you-doing";

substr after 4th occurrence of "-" to get "you-doing" call the function above as

echo substr_after_nth($str, "-", 4);

it will result as

you-doing
Tofeeq
  • 2,523
  • 1
  • 23
  • 20
0

A regex approach will afford an accurate, reliable, and concise solution. I'll demonstrate with an array, but it will work on a single string without modification.

Match all characters in the string until a sequence of slash followed by non-slash characters occurs N times before the end of the string. Capture those final characters as a single group, then replace the entire string with only those characters.

Code: (Demo)

$strings = [
    'this/is/the/path/to/my/fileA.php',
    'this/could/also/be/the/path/to/my/fileB.php',
    'another/example/of/a/long/address/which/is/a/path/to/my/fileC.php',
];

var_export(
    preg_replace(
        '~.*((?:/[^/]*){4}$)~',
        '$1',
        $strings
    )
);

Output:

array (
  0 => '/path/to/my/fileA.php',
  1 => '/path/to/my/fileB.php',
  2 => '/path/to/my/fileC.php',
)

The inverse of this action is to match the unwanted tail-end characters and replace them with an empty string. (Demo)

var_export(
    preg_replace(
        '~(?:/[^/]*){4}$~',
        '',
        $strings
    )
);

That result:

array (
  0 => 'this/is/the',
  1 => 'this/could/also/be/the',
  2 => 'another/example/of/a/long/address/which/is/a',
)

While explode-slice-implode is a working solution, I personally don't like making 3 function calls and generating a temporary array with several unwanted elements. (Demo)

echo implode('/', array_slice(explode('/', $string), -4));
// path/to/my/fileA.php

Related content: Strip all characters in a string starting from the second occurring hyphen

mickmackusa
  • 43,625
  • 12
  • 83
  • 136