Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/53793-solved-php-string-functions/
Share on other sites

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?

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.