php


Materialized Path PHP Regex to Select Last Item


I'm trying to get the last occurrence of an item from a materialized path. Below is a subset of possible values I've provided as an example where X, Y & Z can be any arbitrary string:
X/
X/Y/
X/Y/Z/
How can I select that last item on the path using php regex which would output the following for the corresponding lines above:
X
Y
Z
$parts = explode('/', trim($url, '/'));
$lastpart = end($parts);
No need for regex. But if you insist:
#^/?([^/]+)/?$#
Path part is in group 1.
Rather than a regex, use strrpos() to find the last / after trimming off the trailing /:
$string = "/x/y/";
$string = rtrim($string, "/");
echo substr($string, strrpos($string, "/") + 1);
// y
$string = "/x/y/z/";
// prints
// z
$string = "/x";
// prints
// x
I'd go for a preg_split and array_pop:
$test = "a/a/v/";
$test = rtrim($test,'/');
$arr = preg_split('/\//',$test);
$lastelement = array_pop($arr);
var_dump($lastelement);

Related Links

Full-Text Search Function not working at all
WordPress settings page for custom methods
laravel get jobs from redis queue located in another codebase\project?
How to concatenate variable and string that contains () in php
Laravel mysql multiple operations with one DB instance
How to add form data to a bootstrap modal in the same page by using jQuery?
WP - How to get all the comment_post_ID in bulk comment approval
How to grab a random MYSQL Column Value using PHP and echo it?
Secure way to allow a specific PHP script to read/write a directory?
php searching empty fields in the db [duplicate]
PHP strtotime returns incorrect month [duplicate]
Listing out team members with select options
Restart foreach loop in PHP
Can't map request body json into injected response object
Can't access local php website (Linux)
php code not work, need some change [duplicate]

Categories

HOME
yaml
angular-ui-grid
add-on
kivy
slider
bing-search
tcp
biztalk-2010
elk-stack
drag-and-drop
share
joomla3.2
angular2-template
telegraf
cisco
sharepoint-online
graphdb
mailmerge
ex
jodatime
orange-api
backtracking
gollum-wiki
pdf.js
jstree
introspection
fatfs
substance
guice
matlab-app-designer
publishing
series
john-the-ripper
lxml
osx-lion
mongoengine
custom-keyboard
adminer
textmate2
keyboard-layout
spinner
twilio-php
text-decorations
assert
microkernel
suds
hdiv
icepdf
morphline
apache-toree
type-inference
uiautomatorviewer
nusoap
subforms
android-sharing
winmerge
lvalue
swiperefreshlayout
instafeedjs
nrf51
wildfly-9
perl-data-structures
elastic4s
p6spy
node-glob
frame-grab
jemdoc
certificate-authority
juniper-network-connect
openshift-cartridge
ora-04091
wlanapi
ngcordova
green-threads
cache-manifest
karma-coverage
actionpack
console.log
lifetime
ildasm
windows-rt
oracle-adf-mobile
loginview
angular-ui-select
windows-messages
git-reset
sqlbindparameter
air-native-extension
windows-taskbar
particle-swarm
mft
discovery
jquery-transit
delphi-xe4
email-verification
multiscaleimage
cgrectmake
getproperty
uipangesturerecognizer
series-40
fitch-proofs
explicit
cleartype
aspbutton
google-translator-toolkit
spring-modules
activex-exe
synthesizer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App