0

I need to turn this:

/home/me/public_html/folder1/folder2/folder3/script.php

Into:

folder3/script.php

So far, I have tried using:

(\/.+\/.+\.php)$

However the end result is the entire path is returned. What am I doing wrong?

3

2 Answers 2

3
$path='/home/me/public_html/folder1/folder2/folder3/script.php';
$pathinfo=pathinfo($path);
$a=explode(DIRECTORY_SEPARATOR,$pathinfo['dirname']);
$dir=end($a);
echo $dir.DIRECTORY_SEPARATOR.$pathinfo['basename'];
Sign up to request clarification or add additional context in comments.

5 Comments

I think using explode('/'.. is simpler
Simpler, but not cross-platform.
@coladict For example ...?
@Mohammad Windows CLI.
Good job I'm using Linux then, as per the directory structure in the initial post!
0

<?php
$pathinfo=pathinfo('/home/me/public_html/folder1/folder2/folder3/script.php');
$a=explode('/',$pathinfo['dirname']);
echo $dir=end($a);
echo $pathinfo['basename'];
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.