Shelby_Polo Posted February 28, 2011 Share Posted February 28, 2011 I'm looking for some help with converting special characters within a string to a different character. Current Code:<?php echo $use['value'] ?> $use is an array['value'] is the string within the array I'm taking the data from $use['value'] and using it in a URL. The problem is if $use['value'] is something like "Home Projects & Hobbies" the URL gets messed up; it will look like /Home%20Projects%20&%20Hobbies. So, if possible, I want to write a little script that will take the spaces of the string and convert them into "-" and take the "&" symbol and convert it into "and". Suggest a poll for Tip.it - Here! Link to comment Share on other sites More sharing options...
obfuscator Posted February 28, 2011 Share Posted February 28, 2011 You'd need to use str_replace: http://php.net/manual/en/function.str-replace.php. What's the goal behind this, might I ask? "It's not a rest for me, it's a rest for the weights." - Dom Mazzetti Link to comment Share on other sites More sharing options...
Shelby_Polo Posted February 28, 2011 Author Share Posted February 28, 2011 I'm using a php loop that creates the links here: Code that makes the links:<a href="<?php echo Mage::getBaseUrl();?><?php echo $use['value'] ?>"><?php echo $this->htmlEscape( $use['value'] ) ?></a> Important part from above:<?php echo $use['value'] ?> $use['value'] is used for the end of the url, for example www.website.com/candlemaking The trouble is when $use['value'] has spaces or & Suggest a poll for Tip.it - Here! Link to comment Share on other sites More sharing options...
obfuscator Posted February 28, 2011 Share Posted February 28, 2011 In html links spaces are automatically replaced by %20...you can substitute dashes but then the link won't work. "It's not a rest for me, it's a rest for the weights." - Dom Mazzetti Link to comment Share on other sites More sharing options...
Shelby_Polo Posted February 28, 2011 Author Share Posted February 28, 2011 Which is what's happening, so instead I want to change spaces :arrow: "-" and "&" :arrow: "and" within the string. Like if I have a page www.website.com/home-projects-and-hobbies/ but the string is "home projects & hobbies", how can I change it to use the dashes (as well as "&" to "and")? Suggest a poll for Tip.it - Here! Link to comment Share on other sites More sharing options...
Shelby_Polo Posted February 28, 2011 Author Share Posted February 28, 2011 Just as an fyi, this worked; thanks John. <?php $str = $use['value']; $order = array(' ', '&'); $replace = array('-', 'and'); $newstr = str_replace($order, $replace, $str); echo $newstr; ?> Suggest a poll for Tip.it - Here! Link to comment Share on other sites More sharing options...
obfuscator Posted February 28, 2011 Share Posted February 28, 2011 Just as an fyi, this worked; thanks John. <?php $str = $use['value']; $order = array(' ', '&'); $replace = array('-', 'and'); $newstr = str_replace($order, $replace, $str); echo $newstr; ?>Np. "It's not a rest for me, it's a rest for the weights." - Dom Mazzetti Link to comment Share on other sites More sharing options...
Markup Posted March 1, 2011 Share Posted March 1, 2011 http://en.wikipedia.org/wiki/Percent-encodinghttp://php.net/manual/en/function.urlencode.phphttp://php.net/manual/en/function.urldecode.php Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now