View Full Version : Using preg_replace to put a space after letters (before numbers)

12-02-2010, 01:28 AM
I have an input field that I am trying to make sure is formatted correctly. It is getting class codes (i.e. ARTS 101) and inputting them to a database. I want to first check that there is a space between the characters and the numbers (so ARTS 101 as opposed to ARTS101). Is this the correct preg_replace to do that?

$code = preg_replace('^([A-Z]+)','^([A-Z]+) ',$input);

12-02-2010, 01:45 AM
Is there a reason you can't use a drop down menu? This is the perfect use for one.

12-02-2010, 01:49 AM
I am already using an autosuggest drop down. The problem is there are going to be thousands of codes, and I want the user to be able to add a code if it isn't already in the database.

12-02-2010, 03:21 AM
This is untested, but it should accomplish what you're looking for:

$input = preg_replace( '/([a-z])(\d)/i', '$1 $2', $input );

12-02-2010, 04:30 AM
That code outputs the same thing as before!

EDIT: Nevermind, it works. My conditional was incorrect! Thanks!