www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to put spaces in a string?

  1. #1
    Join Date
    Aug 2006
    Posts
    255

    How to put spaces in a string?

    Let's say I have a string, that contains random numbers and letters:

    PHP Code:
    $string '12hd67dgbf40dkfgeqw647nkd873d73'
    I would like to put spaces in this string after every 4th character, so that the sting would look like:

    12hd 67dg bf40 dkfg eqw6 47nk d873 d73

    The technique would need to work on strings of any length.

    I have some ideas, but they're all pretty rough. I am guessing there must be some PHP function I could use to help me.

    Any suggestions?

  2. #2
    Join Date
    Aug 2006
    Posts
    255
    Got it:
    PHP Code:
    $cc '12345678901234567890';

    $i=4;

    while ((
    substr($cc$i)) != NULL) {
        
        
    $cc substr($cc0$i) . ' ' substr($cc$i);
        
        
    $i = ($i 5);


  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    An alternative:
    PHP Code:
    $cc trim(chunk_split($cc4' ')); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles