php - How to get a portion of a string with a regular expression -
i have situation can have strings this:
"project\\v1\\rest\\car\\controller" "project\\v1\\rest\\boat\\controller" "project\\action\\truck" "project\\v1\\rest\\helicopter\\controller" "parental\\boat\\action"
just in case string follow pattern:
"project\\v1\\rest\\the_desired_word\\controller"
i want the_desired_word.
that's why i'm thinking in regular expression.
to use regular expression, need escape slash twice : once php string , once regex
try :
$tab = array( "project\\v1\\rest\\car\\controller", "project\\v1\\rest\\boat\\controller", "project\\v1\\rest\\helicopter\\controller", "project\\v1\\rest\\water\\controller", ); foreach ($tab $s) { preg_match("!\\\\([^\\\\]*)\\\\controller!u", $s, $result); var_dump($result); }
Comments
Post a Comment