str_replace problem

Discussion in 'PHP' started by Icheb, Sep 16, 2006.

  1. #1
    When you use code like this

    echo str_replace('- ', '-', '1 – 1');
    PHP:
    you would expect the output to be 1 –1, right? However my output is unaffected by str_replace and I have no clue what the problem is.
     
    Icheb, Sep 16, 2006 IP
  2. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try retyping ... are those minus signs ?? coz it worked when i tried it
     
    VONRAT, Sep 16, 2006 IP
  3. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's a hyphen and well, it doesn't work here.
     
    Icheb, Sep 16, 2006 IP
  4. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    :confused:

    try copying this

    
    <?
    	echo str_replace('- ', '-', '1 – 1');  # your original line
    	echo '<Br>'.'1 - 1'.'<Br>';
    	echo str_replace(' – ', '–', '1 – 1'); # hehe my line
    ?>
    
    Code (markup):
     
    VONRAT, Sep 16, 2006 IP
  5. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    1 – 1<Br>1 - 1<Br>1–1

    That's the output.
     
    Icheb, Sep 16, 2006 IP
  6. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yikes ... it should have been
    
    echo str_replace('– ', '–', '1 – 1');
    
    Code (markup):
    not
    
    echo str_replace(' – ', '–', '1 – 1'); # hehe my line
    
    Code (markup):
    see any difference :D
     
    VONRAT, Sep 16, 2006 IP
  7. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Does it have something to do with character codes or something? Because apparently it works sometimes and sometimes not and I'm about to tear my hair out because I can't figure out why.
     
    Icheb, Sep 16, 2006 IP
  8. VONRAT

    VONRAT Banned

    Messages:
    181
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yeah ... those symbols look the same to the human eye but they have different ASCII values :D

    hmmm im gonna give that problem to my officemates .. see how fast they figure it out .. :D
     
    VONRAT, Sep 16, 2006 IP
  9. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I am using the same key for putting that character in the code and for putting it in the form field for which I need this code. Yet it doesn't work.
     
    Icheb, Sep 17, 2006 IP
  10. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    How is it possible that no one knows what's wrong here?
     
    Icheb, Sep 17, 2006 IP
  11. discoverclips

    discoverclips Peon

    Messages:
    491
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #11
    echo preg_replace('/\- /', '-', '1 - 1');
    PHP:
     
    discoverclips, Sep 17, 2006 IP
  12. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #12
    That's no different to what I wrote initially. Basically I'm trying to convert a title so that it can be displayed in an url and for that I am converting spaces to hyphens. With something like "bla - bla" I would end up with "---" and for some reason neither replacing --- with - nor "- " with - works.
     
    Icheb, Sep 17, 2006 IP
  13. GeorgeB.

    GeorgeB. Notable Member

    Messages:
    5,695
    Likes Received:
    288
    Best Answers:
    0
    Trophy Points:
    280
    #13
    How about simply removing the hyphens first, then creating URLs?

    $title_tag = "My Three-Sons";

    $title_tag = str_replace("-", " ", $title_tag);
    $title_now = str_replace(" ", "-", $title_tag);

    echo $title_now; // My-Three-Sons

    Seems simple enough.
     
    GeorgeB., Sep 17, 2006 IP
  14. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #14
    You are right. He is using "&ndash;" in the 1 - 1 part. In html view, his code is displayed as
    @Icheb, try out this instead of your original one and let us know the result.:D
    echo str_replace('- ', '-', '1 - 1');
    PHP:
     
    vishwaa, Sep 17, 2006 IP
  15. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Ok, the only way I can get this to work is when I copy & paste the hyphens that the script outputs and then use that in the search rule. It's like it just doesn't recognize the hyphens any other way. It's also not related to the HTML entities, because they would show up in my editor.
     
    Icheb, Sep 17, 2006 IP
  16. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #16
    Have you looked at the variable passed from the form for URL Encoded Chars?
     
    noppid, Sep 17, 2006 IP
  17. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #17
    It gets saved as normal hyphens in the database, so it's nothing like that either I'm afraid.
     
    Icheb, Sep 17, 2006 IP
  18. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #18
    I would want to see the character encoding set on the display page and echo the variable before the str_replace and after the replace.

    This seems too simple to be beating you up.
     
    noppid, Sep 17, 2006 IP
  19. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #19
    
    <?php
    
    // works as expected
    $strg = '1 - 1';
    echo $strg;
    echo '<br />';
    $test = str_replace('- ', '-', $strg);
    echo $test .'<br />';
    echo strlen($test);
    echo '<br /><br />';
    
    // don't work
    echo str_replace('- ', '-', '1 – 1');
    echo '<br />';
    echo strlen('1 – 1');
    echo '<br />';
    echo strlen(str_replace('- ', '-', '1 – 1'));
    echo '<br /><br />';
    
    // don't work
    $result = str_replace("- ", "-", "1 – 1");
    echo $result . '<br />';
    echo strlen("1 – 1");
    echo '<br />';
    echo strlen($result);
    echo '<br /><br />';
    
    // let's look at the bad handling, char in quotes
    echo "char - #45: " . chr(45) . '<br  />';
    echo "Our Bad char Ascii: " . ord(substr( '1 – 1',2,1)) . '<br  />';
    echo "Our Bad char text: " .substr( '1 – 1',2,1). '<br  />';
    echo "char - #150: " . chr(150) . '<br  /><br />';
    
    // let's look at the good handling, char in varable after being stored from quotes.
    echo "char - #45: " . chr(45) . '<br  />';
    echo "Our good char Ascii: " . ord(substr( $strg,2,1)) . '<br  />';
    echo "Our good char text: " .substr( $strg,2,1). '<br  />';
    ?>
    
    PHP:
    Output...
    
    1 - 1  :String to search char(45)
    1 -1   :String changed as expected still char(45)
    4       :String Length output affected
    
    1 – 1  :Output char(150)
    5       :String Length output not affected
    
    1 – 1  :Output char(150)
    5       :String Length output not affected
    
    char - #45: -               :Normal width char(45)
    Our Bad char Ascii: 150  :Actual char(150) interpruted in string from quotes
    Our Bad char text: –      :Wide dash char(150) displayed
    char - #150: –              :char(150) from text in quotes
    
    char - #45: -                :Normal char(45) dash 
    Our good char Ascii: 45   :Actual char(45) still interpruted as 45 from variable
    Our good char text: -      : Normal width char(45) from variable
    
    Code (markup):
    Actual test page to see the real results as vB is not showing the result correctly. http://www.cpurigs.com/testcrap/str_rep.php Notice the different width dashes.

    All I can say is that PHP is handling the the characters differently when stored in variables then when passed to functions.
     
    noppid, Sep 17, 2006 IP