str_replace

Discussion in 'PHP' started by zodiac, Feb 18, 2009.

  1. #1
    $sss = '[pagetitle]';
    $ss = str_replace("_", " ", $sss);
     echo $ss;
    PHP:
    it sees it,but wont replace the underscores.
    $sss = "some_thing"; it will.
    
    it shouldn't matter since [pagetitle] passed from $sss to  $ss :confused:
    PHP:

     
    zodiac, Feb 18, 2009 IP
  2. NuLLByTe

    NuLLByTe Active Member

    Messages:
    382
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    80
    #2
    Well this is weird, it works for me.

    [​IMG]
     
    NuLLByTe, Feb 18, 2009 IP
  3. G.B.Yahav

    G.B.Yahav Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I dont really get what you want to do...
    if $sss contains varible '[pagetitle]' its cant replace _ becuse '[pagetitle]' dont have it...

    str_replace replace only values that inside the value you looking for

    try to do this
    
    $var = '[pagetitle]';
    $var2 = str_replace( "_", " ", $var, $count);
    print "var1: $var\n var2: $var2\n count of replaces: $count";
    exit();
    
    PHP:
    it will show you the number of times the str_replace find and replace your searched value.

    hope it'll help ya :)
     
    G.B.Yahav, Feb 18, 2009 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Your $sss does not contains any underscores that's why.

    :: ads2help
     
    ads2help, Feb 19, 2009 IP
  5. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #5
    as a good rule of thumb echo out $sss to see what it contains. Then you know if it contain illegal charactors. else{it wont work}
     
    Funk-woo10, Feb 19, 2009 IP
  6. zodiac

    zodiac Peon

    Messages:
    2,661
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    0
    #6
    [pagetitle] is the function that pulls some_thing from the database.
    my code works,but it doesn't remove the underscore from some_thing.

    if i replace $sss = '[pagetitle]'; with $sss = "some_thing"; it removes the underscore.
     
    zodiac, Feb 19, 2009 IP
  7. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think it is still a grave mystery to everyone what you are talking about. Is [pagetitle] supposed to be some sort of placeholder for an actual function call, for the purposes of this forum post? If so, putting quotes around it is a very confusing way to present it to us.

    Why don't you echo $sss immediately prior to the str_replace() call to see if it really contains what you thought it did?
     
    SmallPotatoes, Feb 19, 2009 IP
  8. my gene

    my gene Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    $sss = '[pagetitle]';
    $find = "_";
    if (strpos($sss, $find) === FALSE)
    { 
      var_dump($sss);
    }
    
    else str_replace("_", " ", $sss);
    
    PHP:
    Try this, should help debugging.
     
    my gene, Feb 19, 2009 IP
  9. zodiac

    zodiac Peon

    Messages:
    2,661
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    0
    #9
    before
    <h2>[pagetitle]</h2>
    PHP:
    after
    
    <?
    $sss = '[pagetitle]';
    $ss = str_replace("_", " ", $sss);
    ?>
    <h2><?php  echo $ss; ?></h2>
    
    PHP:
    exact coding.
    both work the same,but the second one doesn't remove the underscore.

    i'm not sure where the pagetitle is pulled from the functions.
    [pagetitle] is like an echo,so i don't get why it doesn't replace the underscores,unless it has to be on the $ in the functions.
     
    zodiac, Feb 20, 2009 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I don't know what you think square brackets do, but I can assure you they don't do it. Maybe you are thinking of {curly brackets}?

    $sss = '[pagetitle'];

    has exactly one effect: that is, it puts the characters

    [ p a g e t i t l e ]

    in the variable $sss. Nothing else.

    Your str_replace will never do anything because there is no underscore in that string ("[pagetitle]").
     
    SmallPotatoes, Feb 21, 2009 IP
  11. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #11
    Okay. Well, firstly I'm going to assume you use a templating class to parse the file (and hence fill [pagetitle] with data that DOES include underscores.)

    Please post this templating script.

    If you do NOT use a templating script, WHICH UNDERSCORE?
     
    Danltn, Feb 21, 2009 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    What you mean is your [pagetitle] is not a string??

    - ads2help
     
    ads2help, Feb 21, 2009 IP
  13. NFreak

    NFreak Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    If that is your "before" and "after", I'd go with the "before", as the "after" is just a waste of PHP. The code that you just gave is going to take the string "[pagetitle]" and replace the underscores with spaces, doing absolutely nothing in the process.

    There must be something that I am missing, because there is no point in removing underscores from a string that you physically coded into a variable yourself, anyways.

    I am really trying to figure out what is going on here. If you are thinking that '[pagetitle]' is going to do something magical and produce you some underscores, taking the single-quotes off of it would help. But that's not even the case.

    Good luck with your code.
     
    NFreak, Feb 22, 2009 IP