Results 1 to 3 of 3

Thread: How to put spaces in a string?

  1. #1
    Join Date
    Aug 2006

    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
    Got it:
    PHP Code:
    $cc '12345678901234567890';


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

  3. #3
    Join Date
    Aug 2004
    An alternative:
    PHP Code:
    $cc trim(chunk_split($cc4' ')); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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