emexinc Posted March 1, 2010 Share Posted March 1, 2010 ...I'm trying to split up a string, and if there is any letters before numbers, then those letters need to be capitalized, and if there are any letters after the last number, then those need to be lower cased... ...such as the below... STRING Output m303m M303m 303M 303m m303-1m M303-1m m12x3m M12x3m ...the letters of course will be different all the time, as well as the fact that there may or may not be letters at the start of ending of the string...this is what i have so far, but it's not working with all the test strings... $test = "m303-3m"; preg_match("/^(\w+\d)(\D+)$/i",$test,$m); $a = strtoupper($m[1]); $b = strtolower($m[2]); echo $a.$b; ...thanks for your help...darwin Quote Link to comment Share on other sites More sharing options...
ram4nd Posted March 1, 2010 Share Posted March 1, 2010 Why you use preg_match, its slow and if you don't know how to do this then use php functions. Really simple task, look into string function on php.net Quote Link to comment Share on other sites More sharing options...
cags Posted March 1, 2010 Share Posted March 1, 2010 $output = preg_replace_callback('#([a-z])(.*)([a-z])#i', 'magicFunction', $input); function magicFunction($matches) { return strtoupper($matches[1]).$matches[2].strtolower($matches[3]); } Quote Link to comment Share on other sites More sharing options...
emexinc Posted March 2, 2010 Author Share Posted March 2, 2010 Why you use preg_match, its slow and if you don't know how to do this then use php functions. Really simple task, look into string function on php.net ...could your response be any more general?...I would of rather had you ignore my question...and the last time i checked, preg_replace is a php function...thanks a lot ram4nd... Quote Link to comment Share on other sites More sharing options...
ram4nd Posted March 2, 2010 Share Posted March 2, 2010 Why you use preg_match, its slow and if you don't know how to do this then use php functions. Really simple task, look into string function on php.net ...could your response be any more general?...I would of rather had you ignore my question...and the last time i checked, preg_replace is a php function...thanks a lot ram4nd... Ok, 1) strtolower, so you get all your letters to lower case 2) if(is_numeric(str[1])) str[0] = strtoupper(str[0]); Something like this... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.