insert str_replace

Discussion in 'PHP' started by wahyuagung, Aug 1, 2012.

  1. #1
    Hi,
    I'm new with php. Please help.
    I have two lines php code. How do I insert:

    str_replace(' ','+',$term->value)
    Code (markup):
    into this line:

    $toReturn .= $options['pre_term']."<a href=\"http://domain.com/$term->value\\">$term->value</a>";
    Code (markup):
    Thanks for help :)
     
    wahyuagung, Aug 1, 2012 IP
  2. Alejandro131

    Alejandro131 Greenhorn

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    21
    #2
    After reading from the PHP documentation what the str_replace function does and returns you have 2 options here:

    1st if you want to "merge" the two lines of code into one you could just replace all occurrences of
    $term->value
    Code (markup):
    with
    str_replace(' ','+',$term->value)
    Code (markup):
    which will give you the following new line of code:
    $toReturn .= $options['pre_term']."<a href=\"http://domain.com/".str_replace(' ','+',$term->value)."\\">".str_replace(' ','+',$term->value)."</a>";
    Code (markup):
    However I would advise against writing it that way as it makes the code less readable and if you come back later and look at it you could spend some time wondering what you are doing, if your code/text editor doesn't support highlights.

    Your 2nd option is to store the string with replaced symbols in a new variable or just reuse the old one which would give you the following 2 lines of code:
    
    $term->value = str_replace(' ','+',$term->value);
    $toReturn .= $options['pre_term']."<a href=\"http://domain.com/".$term->value."\\">".$term->value."</a>";
    
    Code (markup):
    Use whatever makes you comfortable, but if the string is too long an "optimization" would be to call the str_replace function only once and use the 2nd option.
     
    Alejandro131, Aug 1, 2012 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    You could use url_encode if you are having trouble creating a valid url.
     
    jestep, Aug 1, 2012 IP
  4. wahyuagung

    wahyuagung Active Member

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    It works.
    I use this code

    $toReturn .= $options['pre_term']."<a href=\"http://domain.com/".str_replace(' ','+',$term->value)."\">$term->value</a>";
    Code (markup):
    Thanks Guys!
     
    wahyuagung, Aug 1, 2012 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    1) that replacement looks an awful lot like a urlencode

    2) do you only want to apply it to the anchor and not the text? I'd assume so.

    3) if you stop using double quotes on strings, you get faster and cleaner code that's easier to maintain.

    4) if that's a user side result it may have reserved html chars in it, so you may also want to htmlspecialchars the CDATA.

    So... assuming all of the above is what you are trying to do...
    
    $toReturn.=$options['pre_term'].'
    	<a href="http://domain.com/'.urlencode($term->value).'">
    		'.htmlSpecialChars($term->value).'
    	</a>';
    
    Code (markup):
    Is how I'd approach that... again, assuming you want to make $term->value safe to pass as a URL for the anchor and safe to pass as CDATA inside the anchor. On the flip side, use urldecode to turn it back into proper plaintext.
     
    deathshadow, Aug 2, 2012 IP
  6. wahyuagung

    wahyuagung Active Member

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    I only apply it to the URL, not the text. Yes, it is user search result. Can I add strtolower to this htmlSpecialChars line?
    Btw, thanks Dude! :)
     
    wahyuagung, Aug 3, 2012 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Either before or after the encode... I'd suggest before (so htmlspecialchars is wrapping it) since there's typically less characters.

    '.htmlSpecialChars(strtolower($term->value)).'

    Not real tricky.
     
    deathshadow, Aug 3, 2012 IP
  8. sources68

    sources68 Member

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #8
    You can write it after
    You can more view : PHP Tutorials, Tips and Tricks
     
    sources68, Aug 3, 2012 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    @sources68, you do know what urlencode does, right? Just laughing that your sending the swap of spaces to plus to urlencode, which will then change those plusses to % codes...

    oopsy.
     
    deathshadow, Aug 4, 2012 IP
  10. wahyuagung

    wahyuagung Active Member

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10
    I think I have the right answer :cool: Thank you
     
    wahyuagung, Aug 6, 2012 IP