doforumda Posted February 16, 2010 Share Posted February 16, 2010 hi how can we replace spaces between words to comma e,g "first second third" replace to "first,second,third" and how can we make first letter of each word after comma capital e.g, "first,second,third" to "First,Second,Third" Quote Link to comment Share on other sites More sharing options...
grlayouts Posted February 16, 2010 Share Posted February 16, 2010 do you have a code we could look at? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2010 Share Posted February 16, 2010 $string = "first second third"; echo preg_replace('/[ ]+/', ',', ucwords($string)); //Output: First,Second,Third Quote Link to comment Share on other sites More sharing options...
doforumda Posted February 16, 2010 Author Share Posted February 16, 2010 $string = "first second third"; echo preg_replace('/[ ]+/', ',', ucwords($string)); //Output: First,Second,Third great thats working thanks now what if there is string with only commas between words in that string so can we capital first letter of each word after comma. the above example works but it first remove space and capitalize first letter Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2010 Share Posted February 16, 2010 $string = "first,second third , fourth"; echo preg_replace('/[ |,]+/', ',', ucwords(preg_replace('/,/', ' ', $string))); //Output: First,Second,Third,Fourth 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.