Solved

Find text after last slash - PHP/REGEX

Posted on 2009-05-19
4
912 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 400 total points
Comment Utility
/ is a slash, \ is a backslash - a completely different character.

Maybe try
basename($file)
0
 
LVL 10

Assisted Solution

by:ollyatstithians
ollyatstithians earned 100 total points
Comment Utility
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
Comment Utility
... or use basename() like thehagman said.


Olly.
0
 
LVL 16

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now