Results 1 to 7 of 7

Thread: replace the second char in a string.

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default replace the second char in a string.

    How can I replace the second character in a string with another character?

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    sorry dumb question.

    Code:
    $string="blah"
    $string=substr_replace($string,"",1,1);
    Last edited by james438; 02-25-2008 at 05:20 AM. Reason: formatting

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by james438 View Post
    sorry dumb question.
    no such thing as a dumb question... you were just having a brain fart. glad you found the solution

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Though you found a better solution, you can always figure out something like this just using substr():
    $a = $a[0].'X'.substr($a,2);
    In fact, you might be able to just use: $a[1]='X';, but I'm not sure if that will cause any trouble with the variable type.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    james438 (02-26-2008)

  6. #5
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    You can use brackets as if it were an array. After all, a string is really an array of characters with numerical indices.
    PHP Code:
    $string[1] = ' '
    This replaces the 2nd letter of the variable "string" with a space.

  7. The Following User Says Thank You to Leafy For This Useful Post:

    james438 (02-26-2008)

  8. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    those are some really interesting alternatives. I still plan on using the built in php function substr_replace(), but I really like the work around solutions both of you have posted.

  9. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    And here we find yet another beautiful thing about PHP: there's always so many ways to do things.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

Bookmarks

Posting Permissions

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