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
Share on other sites

$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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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