tags:

views:

37

answers:

1

I have a $delete path and a $user_id each include '#' and '@' respectively within the string. How can I replace each with the encoded value %23 and $40.

I tired using str_replace but did not have any luck: str_replace($string, array('@', '#'), array('%40', '%23'));

My full Delete path looks like this:

$delete = "http://admin:[email protected]/@api/deki/DELETE:users/$user_id/properties/%s";

I feel like the $user_id should be pretty simple. For the properties it has to loop through to get all the available properties. You can see the loop below:

foreach($xml->property as $property) {
      $name = $property['name']; // the name is stored in the attribute
      curl_fetch(sprintf($delete, $name),'admin','12345');
    }

Each property contains a '#', so is there a way that each iteration which modify '#' to be the appropriate value?

Thanks in advance.

A: 

You use str_replace wrongly: The first parameter is search, the second replace and the third subject:

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

So:

str_replace(array('@', '#'), array('%40', '%23'), $string)
Gumbo
@Gumbo I just tried this directly after assigning the $user_id, and after the str_replace I printed it. For some reason, it's still giving me the user_id without the %40 as it should. Any ideas?
Aaron
@Aaron: Did you reassign the modified value to the variable?
Gumbo
@Gumbo Ah, I think that's my issue. So does that mean I need to do $user_id = str_replace(...) ?
Aaron
@Aaron: `str_replace` does work with a copy of the variables value and not the variable itself. Otherwise it would need to be passed as reference.
Gumbo
@Gumbo So how would I correctly work with a copy of the variables value?
Aaron
@Aaron: Just assign the returned value of `str_replace` back to your variable. So `$string = str_replace(…);`.
Gumbo
@Gumbo Cool, that worked. I was being dumb and assigned it back to user_id. Would I be able to use this on the $delete as well? I'm just not sure where it belongs.
Aaron
@Gumbo: I figured out where to str_replace for the properties. One things thats strange, I started getting a warning: springf(): too few arguments. Is this coming from the % possibly? Nothing else has changed. If you have any ideas I'd really appreciate it.
Aaron