SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing Values in Variable

    I'm sorry, I wasn't sure what to entitle this.

    What I need to do is say someone fills out a form and posts it...

    hey

    my name is joe.
    And I want it so that whenever "joe" shows up to replace it with JOE before inserting it into the database, how would I do this?

    (edit the specific value without touching the rest)

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want to use the strtoupper function.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could also look into the ucfirst and ucwords function if you didn't want to capitalize the whole string.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I guess my example wasn't very good.

    I don't necessarily want to upper case anything. I want to replace certain characters with other characters.

    maybe if like $example =
    hi, guys!!!
    and I wanted to replace all exclamation points with periods so that $example would look like:
    hi, guys...
    Do you understand what I mean?

    Sorry about misleading you, but thanks for trying tohelp!

  5. #5
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In that case, you want to use str_replace.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  6. #6
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An example:
    PHP Code:
    $var "Hello guys!!!";
    $var str_replace("!""."$var); 
    This would take every ! and turn it into a ., so you would get Hello guys...
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much.

    Is there a way to check if the value has something and instead of replacing it, stopping the function and saying "Invalid Character" or something similar? Also, can you do str_replace with multiple characters to replace and what to replace them with at the same time, or would you have to do multiple functions?

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way to check if the value has something and instead of replacing it, stopping the function and saying "Invalid Character" or something similar?
    Ya, you could do something like this:

    PHP Code:
    $var "Hello guys!!!"
    if(
    strstr($var'!')) {
         echo 
    'Invalid character; exclamation points not allowed.';
         
    // or if you really want to put the breaks on the script, you could replace the previous line with die('Invalid character; exclamation points not allowed.')

    Also, can you do str_replace with multiple characters to replace and what to replace them with at the same time, or would you have to do multiple functions?
    Yup:
    PHP Code:

    str_replace
    ($bad_chars$safe_chars$var); 
    str_replace allows you to use arrays for the search and replace values. In the above example, if they are both arrays and the same length, then each element in $bad_chars will be replaced by the corresponding entry in $safe_chars. If only $bad_chars is an array, then every element in it that is found in $var will be replaced by the value of $safe_chars. (If both $bad and $safe_chars are arrays, and $safe_chars is shorter than $bad_chars, then every entry in $bad for which there isn't a corresponding value in $safe will be replaced with the empty string ''. $var can also be an array of variables you want to search).

    *Could also use if(strpos($var, '!') !== FALSE) { ... instead of strstr, and maybe should since this method is a bit faster.
    Last edited by k0ng; May 5, 2005 at 09:31.


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
  •