Remove parts of a file path with PHP

Hello!

I currently have a file path (Such as “/home/type_04/domain.com/account_30531134/plate” or “/home/type_02/newdomain.net/account_80233144/subdomain.newdomain.net/plate”).
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.

Visualization:

Before After (Edited Path) After (Type Section)
/home/type_04/domain.com/account_30531134/plate /plate type_04
/home/type_02/newdomain.net/account_80233144/subdomain.newdomain.net/plate /subdomain.newdomain.net/plate type_02
NOTE: “plate” is not a file, it is a folder.
Thanks!
<?php

$originalPaths = [
    '/home/type_04/domain.com/account_30531134/plate',
    '/home/type_02/newdomain.net/account_80233144/subdomain.newdomain.net/plate',
];

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.

<?php
$pathone = '/home/type_04/domain.com/account_30531134/plate';
$pathtwo = '/home/type_02/newdomain.net/account_80233144/subdomain.newdomain.net/plate';

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


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

echo $pathonefinal;
echo $pathtwofinal;

?>

Thanks

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:

https://www.php.net/manual/en/function.strstr.php

also

https://www.php.net/manual/en/function.strpos.php

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 = [
	'/home/type_04/domain.com/account_12345678/plate',
	'/home/type_02/newdomain.net/account_42424242/subdomain.newdomain.net/plate',
	'/home/type_99/domain.com/BAD_PATH', 
];

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>';
endforeach;   


//==========================================
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[] = 'subdomain.newdomain.net/plate';
	else:
		$result[] = 'PROBLEM';
		$result[] = 'INVALID_TYPE';
	endif;

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

Result:

$tmp[0] -> type ==> type_04
$tmp[1] -> result ==> subdomain.newdomain.net/plate
$tmp[2] -> $path ==> /home/type_04/domain.com/account_12345678/plate

$tmp[0] -> type ==> type_02
$tmp[1] -> result ==> path
$tmp[2] -> $path ==> /home/type_02/newdomain.net/account_42424242/subdomain.newdomain.net/plate

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

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.

Example:

<?php
$pathone = '/home/type_04/domain.com/account_30531134/plate';
$pathtwo = '/home/type_02/newdomain.net/account_80233144/subdomain.newdomain.net/plate';

$pathoneending = //Some Code here to get "/plate"
$pathtwoending = //Some Code here to get "/subdomain.newdomain.net/plate"

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


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

?>

Thanks!

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 /subdomain.newdomain.net/plate
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];

4 Likes

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

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.