Jump to content

preg_match issues


emexinc

Recommended Posts

...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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.