Need to find and replace a specific HEX character (hex 'AO' to hex '20')

Discussion in 'PHP' started by KLB, Jul 28, 2006.

  1. #1
    Okay, I need a REALLY goofy PHP based character search and replace I need to do. I need PHP to look at a string and replace all instances of the hexadecimal character 'AO' with the hexadecimal character '20'.

    Both of these hex characters look like a space to you and I but computers see these as two different characters and it is playing havoc in Internet Explorer with a search function I use on my site. No matter what I try I can't seem to get PHP to find the hex character 'AO' and then replace it with '20'. Any help would be greatly appreciated. This really is a bizarre problem.
     
    KLB, Jul 28, 2006 IP
  2. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #2
    I don't know if it will work cause I don't understand your problem but it doesn't hurt to try it out:
    
    $oldstring = "AOAOAO AO";
    $newstring = str_replace("AO", "20", $oldstring);
    
    echo $newstring; 
    // this would return: 202020 20 
    
    Code (markup):
     
    danielbruzual, Jul 28, 2006 IP
  3. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think what you really want to replace is "\xA0". You might need to use preg_replace('/\xA0/', ' ', $input) instead.

    Cryo.
     
    Cryogenius, Jul 28, 2006 IP
    KLB likes this.
  4. KLB

    KLB Peon

    Messages:
    1,167
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Cryogenius has the right answer. - Thanks.

    danielbruzual, your answer only works for replacing those characters if they occur as real characters. My problem was that we were looking at two different hexadecimal codes for essentially the same character (in this case a space).

    I ended up having to add the following code

    $strLink= preg_replace("/\s/"," ",$strLink);
    $strLink= preg_replace("/\xa0/"," ",$strLink);

    There were some other replacements I had to do, and I still have to monitor for any bad side effects but I think I'm going in the right direction.
     
    KLB, Jul 28, 2006 IP
  5. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You dont need to use regular expressions. You can use str_replace itself. Like this:

    str_replace("\xAO", "\x20", $oldstring);

    Thomas
     
    coderlinks, Jul 28, 2006 IP
    KLB likes this.