[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find text after last slash - PHP/REGEX

Posted on 2009-05-19
4
Medium Priority
?
973 Views
Last Modified: 2012-05-07
What is the best way to find the text after the last slash?

/ or \
<?php
 
$file = '/dsafdsf/adsfadsf/asdfdasf/adsf/test.txt';
echo trim(str_replace('_', ' ' , substr($file, strrpos($file,'/') + 1)));
 
echo '<hr />';
 
$file = 'c:\\dsafdsf\\adsfadsf\\asdfdasf\\adsf\\test.txt';
echo trim(str_replace('_', ' ' , substr($file, strrpos($file,'/') + 1)));
 
?>

Open in new window

0
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
thehagman earned 1600 total points
ID: 24420365
/ is a slash, \ is a backslash - a completely different character.

Maybe try
basename($file)
0
 
LVL 10

Assisted Solution

by:ollyatstithians
ollyatstithians earned 400 total points
ID: 24420383
Here is a regex that returns anything between the last / or \ and the end of the string.

Olly.
[^\/]*$

Open in new window

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24420393
... or use basename() like thehagman said.


Olly.
0
 
LVL 16

Author Closing Comment

by:hankknight
ID: 31582926
basename() --- Why didn't I think of that?  Thanks for keeping it simple.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question