Reputation: 6509
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
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
Reputation: 159
explode()
will split string by \
and
array_pop()
gets last array's item
$lastItem= array_pop(explode('\\', $row_data[0]));
Upvotes: 0
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
Reputation: 3633
Add \
$row_data[0] = "\Documents\Images\File";
echo $end = substr(strrchr($row_data[0], '\\'), 1);
Upvotes: 1
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