rastrol7 Posted May 31, 2007 Share Posted May 31, 2007 I'm trying to find the correct function or use of the str_replace() function to substitute every space (' ') in my code with an ampersand. I've tried doing: str_replace(' ', '&', $input) and several variations to no success. Is there an easy way to do this that I've just missed? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/53793-solved-php-string-functions/ Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 www.php.net/str_replace <?php $input = str_replace(' ', '&', $input); ?> Have you tried it like that if that does not work try this: <?php $input = str_replace(' ', '\&', $input); // maybe the amersand needs escaped? ?> Quote Link to comment https://forums.phpfreaks.com/topic/53793-solved-php-string-functions/#findComment-265877 Share on other sites More sharing options...
rastrol7 Posted June 1, 2007 Author Share Posted June 1, 2007 Nope I've tried all the tricks I could on the ampersand. I think the problem is in reading ' ' as a space. The function works for any other character in the string. Quote Link to comment https://forums.phpfreaks.com/topic/53793-solved-php-string-functions/#findComment-265953 Share on other sites More sharing options...
Caesar Posted June 1, 2007 Share Posted June 1, 2007 Where are you pulling your input from? Is it formatted in any way? str_replace() should work fine the way frost showed. Quote Link to comment https://forums.phpfreaks.com/topic/53793-solved-php-string-functions/#findComment-265954 Share on other sites More sharing options...
rastrol7 Posted June 1, 2007 Author Share Posted June 1, 2007 I'm trying to convert a series of characters in a string into an array. The starting string is: Medium=+0 Large=+0 X-Large=+0 2XB=+1 3XB=+1 4XB=+1 5XB=+2 6XB=+2 7XB=+3 8XB=+3 9/10XB=+4 11/12XB=+8 LT=+1 1XLT=+1 2XLT=+1 3XLT=+1 4XLT=+1 5XLT=+2 6XLT=+2 7/8XLT=+3 9/10XLT=+6 I want to end up with an array that associates each size with the number after the "=+". I know that the following will convert my string into an array where [Medium] => +0, [Large] => 0, etc... <?php parse_str($string, $array); ?> but only if I can have an '&' where each space is. So in short, any ideas on how to add that & or a better way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/53793-solved-php-string-functions/#findComment-265964 Share on other sites More sharing options...
rastrol7 Posted June 1, 2007 Author Share Posted June 1, 2007 Turns out the output from the mysql query wasn't Medium=0 Large=0 X-Large=0 2XB=+1 ... it was actually Medium=0\r\nLarge=0\r\nX-Large=0\r\n2XB=+1 ... the \r\n never showed up as I was developing the site, but once I put in <?php $out = str_replace("\r\n",'&', $out); ?> everything worked fine. Thanks anyway for the help! Quote Link to comment https://forums.phpfreaks.com/topic/53793-solved-php-string-functions/#findComment-265976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.