michaelmcgurk
michaelmcgurk

Reputation: 6509

Get last portion of URL with PHP

In PHP, I have the following string:

$row_data[0] = "\Documents\Images\File";

How do I extract the word File from this string with PHP?

I had tried this but it was unsuccessful.

$end = substr(strrchr($row_data[0], '\'), 1);

Upvotes: 0

Views: 73

Answers (6)

Senthil
Senthil

Reputation: 2246

use double slash for escape character :

echo 'Hello World';

$row_data[0] = 'Documents\\Images\\File';


$end = substr(strrchr($row_data[0], '\\'), 1);

echo $end;

Upvotes: 0

Sergey  Belyakov
Sergey Belyakov

Reputation: 159

explode() will split string by \ and array_pop() gets last array's item

$lastItem= array_pop(explode('\\', $row_data[0]));

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

\ is a escape character so use double slash

<?php

 $row_data[0] = "\Documents\Images\File";
 $end = substr(strrchr($row_data[0], '\\'), 1);
                                     ^^^^^
 echo $end;

?>

Upvotes: 0

Manish
Manish

Reputation: 3633

Add \

$row_data[0] = "\Documents\Images\File";
echo $end = substr(strrchr($row_data[0], '\\'), 1);

Upvotes: 1

Maytyn
Maytyn

Reputation: 122

There are few solutions, I think the one below is easiest, read about pathinfo()

$path   =   '\Documents\Images\File';
$lastPortion    =   pathinfo($path)['basename'];

Upvotes: 0

Rakesh Sojitra
Rakesh Sojitra

Reputation: 3658

     $end=end(explode("\\",$row_data[0]));

Upvotes: 0

Related Questions