3

I have a string called $file_path; for a php gallery, When I make <?php echo $file_path; ?> "gallery/png.png", "gallery/jpg.jpg", "gallery/gif.gif" etc, for all img files on that directory. I want to remove "gallery/" and ".format" for the $file_path;.

4 Answers 4

8
echo basename($file_path,".png");

basename will return only the file name

really a third version of the question, next time work out what you want to ask first please:

echo pathinfo($file_path, PATHINFO_FILENAME);

this will echo "file" if $file_path is "gallery/file.jpg"

Sign up to request clarification or add additional context in comments.

1 Comment

edited answer, dont keep changeling the question please @Kake
2

Try this hope it will work.

$file_path = str_replace('gallery/', '', str_replace('.png', '', $file_path) );

Comments

1

try this

str_replace('gallery/', '', $file_path);

1 Comment

@Kake you can use str_replace('.png', '', $file_path);
0
$url = "gallery/test.png";
$string = str_split($url);
$phase = 0;
$output;
for($i = 0; $i < count($string); $i++){
if($phase==1){if($string[$i]!="."){$output.=$string[$i];}else{break;}}else{if($string[$i]=="/"){$phase++;}}}

echo $output;

Only outputs filename

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.