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 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts

    adding spaces to a text string

    I am reworking an existing web site and part of it involves taking a members name, converting it to all caps and then double spacing the letters. I can display the name in caps using strtoupper() but I have no idea how to add a space between each letter. As it is a 2 word name I would need to double space between the names so easiest way is to place a forced space   between each letter in the name. Is there a simple way I can do this?

    Any help would be appreciated.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Something like this perhaps?

    Code:
     function spaceName($member_name) {
      $name_array = str_split($member_name); //splits $member_name characters
      $spaced_name = "";
      for($i = 0; i < sizeof($name_array); i++) {
       $spaced_name .= $name_array[$i]." ";
      }
    
      return $spaced_name;
     }
    I think that should work... let me know.

    Sadiq.

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    and for those with out php 5

    PHP Code:
    <?php
    function spaceName($member_name) {
    for(
    $i=0;$i<strlen($member_name);$i++)
        {
        
    $name_array[]=substr($member_name,$i,1);
        }
      
    $spaced_name implode(" ",$name_array);
      return 
    $spaced_name;
     }
    echo 
    spaceName(tommy);
    ?>

  • #4
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks sidney,

    I am getting an error - Parse error: parse error, expecting `')''
    Code:
    function spaceName($member_name) {
      $name_array = str_split($member_name); //splits $member_name characters
      $spaced_name = "";
      for($i = 0; i < sizeof($name_array); i++) { error is on this line
       $spaced_name .= $name_array[$i]." ";
      }
    
      return $spaced_name;
     }
    Any ideas?

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I forgot the $...

    Code:
    function spaceName($member_name) {
     $name_array = str_split($member_name); //splits $member_name characters
     $spaced_name = "";
     for($i = 0; $i < sizeof($name_array); $i++) { error is on this line
      $spaced_name .= $name_array[$i]." ";
     }
    
     return $spaced_name;
    }

  • #6
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    thanks


  •  

    Posting Permissions

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