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 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:
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?
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.
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)?