Solved

Find text after last slash - PHP/REGEX

Posted on 2009-05-19
4
933 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
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 100 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

679 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