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 Link to comment https://forums.phpfreaks.com/topic/193772-preg_match-issues/ 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 Link to comment https://forums.phpfreaks.com/topic/193772-preg_match-issues/#findComment-1019863 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]); } Link to comment https://forums.phpfreaks.com/topic/193772-preg_match-issues/#findComment-1019870 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... Link to comment https://forums.phpfreaks.com/topic/193772-preg_match-issues/#findComment-1020244 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... Link to comment https://forums.phpfreaks.com/topic/193772-preg_match-issues/#findComment-1020412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.