Solved

Find text after last slash - PHP/REGEX

Posted on 2009-05-19
4
937 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 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