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

PHP issue calling static class from another static class
Error while sending HTTP request
PHP seems to add in backslashes when strings are concatenated
Querying Elasticsearch with PHP
MemcacheD multiple data saves per second
Button Form POST Not Working
Parse text file line by line and store data in MYSQL database
ERROR invalid-api-key PHP /CURL for Alchemy api text/TextGetCombinedData
Ionic/AngularJS 2nd request not sending data but getting success answer
Swift method to send email through MVC PHP page is very slow
Laravel 5.2 with PostgreSQL autoincrement not working, id must be filled
How to fix Travis-CI [Composer\Exception\NoSslException] with PHP 5.3.3?
PHP Download the json file
NotFoundHttpException in Handler.php while trying to update
Why won't my substr call work with the → character?
My alert doesn't show up in laravel 5

Categories

HOME
opencart
windows-8.1
openxml
selenium-builder
kivy
filesize
embedded-resource
google-play-services
tibco
save
kurento
bellman-ford
scrape
pm2
google-awareness
jodatime
dhcp
xbox360
fresco
bnf
exe
fosuserbundle
adobe-illustrator
clr
android-custom-view
amazon-mws
jest
eclipse-emf
cell
tracing
morris.js
python-imageio
tilemill
google-play-developer-api
commonsware-cwac
rspec-rails
cloveretl
ftp-server
runtimeexception
ms-solver-foundation
cartopy
google-now
export-to-pdf
yoast
git-tfs
moinmoin
nuclio
head.js
spring.net
subforms
nslayoutconstraint
clrs
.when
degrees
oid
xmlbeans
data-management
fiber
static-methods
processor
distributed-cache
okta-api
wif4.5
teensy
hmisc
replicaset
cache-manifest
unidata
ocmockito
jcheckbox
symja
window-resize
igor
two.js
redpitaya
zend-search-lucene
hotswap
ivalueconverter
gpu-programming
typesafe-stack
django-sites
assetic
application-blocks
eclim
ggts
non-ascii-characters
insertonsubmit
stty
flv
websphere-6.1
gamesalad
opends
quartz-core
differentiation
play2-mini
data-dump
dip
aptitude
ccnet-config
self-tracking-entities
zend-test
spring-modules
database-cloning
acceleration
parentid

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