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
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.
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!
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.
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!
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.
@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.