Remove parts of a file path with PHP


I currently have a file path (Such as “/home/type_04/” or “/home/type_02/”).
I wish to strip out the “account_xxxxx” and everything before it from the path. I do not know how to approach this, since the “type”, “domain”, and “account_xxxxx” sections are different in each path. I currently have the “before” path saved to one variable and wish to save the “after” portion to a different one.

Additionally, I wish to save only the “type” section as a third variable.


Before After (Edited Path) After (Type Section)
/home/type_04/ /plate type_04
/home/type_02/ / type_02

NOTE: “plate” is not a file, it is a folder.


$originalPaths = [

foreach ($originalPaths as $path) {
    preg_match('~^/home/(type_\d+)/[^/]+/account_\d+(/.*)~', $path, $results);
    printf('before = %s, after = %s, type = %s', $path, $results[2], $results[1]);
    echo "\n";

See preg_match in the PHP manual and learn about regular expressions here.

1 Like

Thanks for the response. Is it possible to store these new paths as separate variables?

I made the below off you code, however it creates “11” (Without the quotation marks) for some reason.

$pathone = '/home/type_04/';
$pathtwo = '/home/type_02/';

$pathonefinal = preg_match('~^/home/(type_\d+)/[^/]+/account_\d+(/.*)~', $pathone, $results);

$pathtwofinal = preg_match('~^/home/(type_\d+)/[^/]+/account_\d+(/.*)~', $pathtwo, $results);

echo $pathonefinal;
echo $pathtwofinal;



Try PHP strstr() function which I think could be a solution. I’m tapping on a tablet at the moment and unable to supply examples:


Thanks for that response. The "strstr” function looks closest to what I want, however, since the “account” section changes, I do not know how it would work.

Also, is there is a better way to get just the “type” section out of there?

Thanks again!

is there some reason you can’t use relative paths?

Back on the desktop:

<?php declare(strict_types=1);

$paths = [

foreach($paths as $key => $path) :
   $tmp = getResult($path);

   echo '<br> $tmp[0] ->  type   ==> ' .$tmp[0] ;	
   echo '<br> $tmp[1] ->  result ==> ' .$tmp[1] ;
   echo '<br> $tmp[2] ->  $path  ==> ' .$tmp[2] ;
   echo '<hr>';

function getResult( string $path) : array 
	$result = []; 

	if( strpos($path, 'type_02') ) :
		$result[] = 'type_02';
		$result[] = 'path';
	elseif( strpos($path, 'type_04') ) :	
		$result[] = 'type_04';
		$result[] = '';
		$result[] = 'PROBLEM';
		$result[] = 'INVALID_TYPE';

	$result[] = $path;
	return $result;


$tmp[0] -> type ==> type_04
$tmp[1] -> result ==>
$tmp[2] -> $path ==> /home/type_04/

$tmp[0] -> type ==> type_02
$tmp[1] -> result ==> path
$tmp[2] -> $path ==> /home/type_02/

$tmp[0] -> type ==> PROBLEM
$tmp[1] -> result ==> INVALID_TYPE
$tmp[2] -> $path ==> /home/type_99/

Thanks so much for the reply.

Unfortunately, the “type”, “domain”, and “account” variables are randomly generated (Or inputted by the user however they wish), and I have no way to predict what they might be.

So the block below will not work unfortunately.

Also, I wish to be able to echo a single variable to show only the “type” or ending (Everything after the account variable) separately.


$pathone = '/home/type_04/';
$pathtwo = '/home/type_02/';

$pathoneending = //Some Code here to get "/plate"
$pathtwoending = //Some Code here to get "/"

$pathonetype = //Some Code here to get "type_04"
$pathtwotype = //Some Code here to get "type_02"

echo $pathoneending;
echo $pathtwoending;
echo $pathonetype;
echo $pathtwotype;



What was wrong with the piece of awesomeness that @rpkamp gave you yesterday? It seems to do exactly what you want.

I need to be able to call a variable that has the information stored in it. Like below.

echo $pathoneending; // Print /plate
echo $pathtwoending; // Print /
echo $pathonetype; // Print type_04
echo $pathtwotype; // Print type_02

His routine put those in an array called $results. If you need them in individual variables, just move them in like:
$pathoneending = $results[1];
$pathonetype = $results[2];


Please update the $paths array, with at least a extra dozen paths, run the function and post the failed and the expected results.

I got it working with @rpkamp’s code and @tracknut’s note.

Thanks everyone!

1 Like