regex - PHP: preg_replace() to get "parent" component of NameSpace -


how can use preg_replace() replace function return parent "component" of php namespace?

basically:

input: \base\ent\user; desired output: ent

i've been doing using substr() want convert regex. note: can done without preg_match_all()?

right now, have code parent components:

$s = '\\base\\ent\\user'; print preg_replace('~\\\\[^\\\\]*$~', '', $s); //=> \base\ent 

but want return ent.

thank you!

as rocket hazmat says, explode going better here regex. surprised if it's slower regex.

but, since asked, here's regex solution:

$path = '\base\ent\user'; $search = preg_match('~([^\\\\]+)\\\\[^\\\\]+$~', $path, $matches); if($search) {     $parent = $matches[1]; } else {     $parent = ''; // handles case path just, e.g., "user" } echo $parent; // echos ent 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -