URL strings with dollar sign, problem with str_replace

Hello,

I am try to remove commas from a list of URLs dynamically. The URLs sometimes look like this:

$url = “http://images.site.com/NjcyWDU2MA==/z/bmgfFRRdasdfa332P/$T2eC16J,!yME9fsdR44cOyKhw~~50_1.JPG”;

Some have dollars sign in them, which php interprets as a variable because the string is not surrounded by single quotes (not literal). I am trying to build a single comma separated string of all the image urls, which is why I am trying to remove just the comma from the URL.

Here is the full script:


$urls = array(); // an array of urls
foreach ($url_array as $url) {
  $urls[] = str_replace(',', '', $url); // attempt to remove commas from each url, but php sees dollar sign within an URL as a variable
}
$urls = implode(",", $urls); // final string - comma separated list of urls

In the foreach loop, I’m not sure how to convert each $url variable into a literal string, so that I can use the str_replace to remove that comma or replace it with something else. There is probably a simple solution for this. Please let me know what you think.

Thanks
Kind regards

Might want to use a regular expression and preg_replace: http://us1.php.net/preg_replace

urlencode the strings before you try to manipulate them, and then seek out the URLEncoded Comma character?

Also, more likely your problem is how you’re loading your array to begin with.

Thanks for the responses. I’ve tried preg_replace and it doesn’t work because PHP is throwing an undefined variable error because it’s seeing a dollar sign in the URL and interpreting it as a variable. I’ve tried the URL encode, but it’s the same result. It’s removing the dollar sign and characters in front after I use urldecode to turn it back into a normal URL, because PHP is interpreting the dollar sign as a variable.

What I’m doing is saving images from a downloaded and parsed simpleXML file. Each image is listed in an array, which I need find, clean and build another array, which will be imploded and separated by commas in the end:


foreach ($item->ImageURLs as $image) {
  $url = str_replace(',', '', $image);
  $urls[] = $url;
}
$imageurls = implode(",", $urls);

Is there some way to convert each URL into a literal string before using the str_replace or preg_replace, preventing the dollar sign from executing as a variable?

Thanks
Kind regards

I’m having difficulty understanding why PHP would be throwing an error when you are passing the URL as a string to str_replace or preg_replace. At what point in the process are you getting the error?

Can you provide a pastebin (paste2.org) with an example array of URL’s that include a dollar sign along with the code you are using thus far?

I’m a visual type of person, and I have a feeling I’m not seeing the entire picture.

sounds like the content is enclosed in " " when it ought to be enclosed in ’ ’ so as to not perform variable substitution

The $image variable that gets inserted into the preg_replace will trigger an error in the error reporting log. Basically, the error says that there is a variable (that dollar sign in the URL) within the $image and that it’s undefined.

I was able to figure this out, but since I am importing parsed variables from an simpleXML file, how can I convert whatever is inside the $image variable into the single quotes ’ ’ so that the variable doesn’t execute?

I’m also using simplexml_load_string() to parse the main XML file itself. Maybe there is an option here that parses everything into literal strings (preferable)?

Nevermind, I think I figured it out. The script may have been working correctly all along, but my debugging method was flawed…

Thanks for looking into this. Thread can be closed now.
Kind regards