Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Using preg_replace to put a space after letters (before numbers)

    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?

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

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,036
    Thanks
    15
    Thanked 240 Times in 240 Posts
    Is there a reason you can't use a drop down menu? This is the perfect use for one.

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    This is untested, but it should accomplish what you're looking for:
    PHP Code:
    $input preg_replace'/([a-z])(\d)/i''$1 $2'$input ); 
    Last edited by kbluhm; 12-02-2010 at 02:28 AM.

  • Users who have thanked kbluhm for this post:

    soneen (12-02-2010)

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    54
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That code outputs the same thing as before!

    EDIT: Nevermind, it works. My conditional was incorrect! Thanks!
    Last edited by soneen; 12-02-2010 at 03:33 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •