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 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    String Replace - Multiple Rules?

    I don't know how to describe this so my subject may be a bit confusing.

    I want to use the str_replace function but for multiple variables. For example, if some submits €, I want it to be replaced with € but also if they submit a £ I want it to be replaced with £. So I have the simple code below which does the job for euros. How can I add pounds and maybe even a couple other rules to this string?

    PHP Code:
    str_replace(chr(128), "€"stripslashes($cost)) 

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The parameters can be arrays so you can replace multiple strings in one call.

    http://us2.php.net/manual/en/function.str-replace.php

  • Users who have thanked Fumigator for this post:

    wilcosky (09-19-2009)

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    The parameters can be arrays so you can replace multiple strings in one call.

    http://us2.php.net/manual/en/function.str-replace.php
    So I get that I can use something similar to the code below... but the code below would replace the euro symbol and the pound smybol with the same html. How do I change the code below so that if its a euro character the euro html is used and if it's a pound character the pound html is use?

    PHP Code:
    $currency = array("chr(128)""chr(163)");
    str_replace($currency"€"$cost); 
    That code will replace the character with a euro no matter if it's a euro or a pound...


    I want to say if cost has chr(128) anywhere in it, then change the chr(128) to € and if cost has chr(163) anywhere in it, then change chr(163) to £

    Otherwise if I user inputs a € symbol, it just displays as a question mark. My page is encoded as utf-8. The only way the symbols will display correctly is if I change it to iso-8859-1, however, if I do that, then the symbols show up correctly but some apostrophes turn into weird characters... so no matter what encoding I use, something gets messed up.

    The easiest way for me to fix is to leave the encoding utf-8 (because everything shows up correctly except certain symbols like euros) but change certain symbols to the pure html form when the page is viewed by either using string replace or by creating some type of if else statement maybe...
    Last edited by wilcosky; 09-19-2009 at 03:15 AM.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Example:

    Code:
    $find = array(chr(128), chr(163));
    $replace = array('€', '£');
    
    $output = str_replace($find, $replace, $cost);
    Last edited by MattF; 09-19-2009 at 03:20 AM.

  • Users who have thanked MattF for this post:

    wilcosky (09-19-2009)

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    Example:

    Code:
    $find = array(chr(128), chr(163));
    $replace = array('€', '£');
    
    $output = str_replace($find, $replace, $cost);
    Perfect!


  •  

    Posting Permissions

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