John Davidson

php - searching for a substring of a word that starts with and ends with

0 comments
Message:


I have an array with the names of the controllers of my php app and i need to get the first word out of the name for example i have:


TenantController
TenantMetasController
DealerTenant


I'm trying to retrieve the first substring that starts with an uppercase letter and ends with a lowercase letter , before the next uppercase letter


so if i have "TenantController" i want to retrieve "Tenant" as a prefix


I tried to search for regular expression but it doesnt seem to be of help for what i want to do


Thank you



My Answer:
$controllers = ['TenantController', 'TenantMetasController', 'DealerTenant'];

foreach ($controllers as $controller) {
preg_match('/[A-Z][a-z]+/', $controller, $matches);
echo $matches[0] . "\n";
}

This code snippet uses a regular expression to match the first substring that starts with an uppercase letter and ends with a lowercase letter in each controller name. The preg_match function is used to perform the matching, and the result is stored in the $matches array. Finally, the matched substring is printed out for each controller.

Rate this post

5 of 5 based on 3651 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us