Trouble replacing an ampersand ( & ) in PHP with str_replace – Solution
Now this took about a half an hour until I figured this out. Posting this, just in case someone finds it – I hope I save you 30 minutes.
I fetched a link from HTML content with preg_match(), and I had to replace some data in that link.
str_replace("upgrade-plugin&plugin",'update-selected&plugins', $updateItem);
For some reason – the ampersand ( & ) wasn’t found and so the string wasn’t replacing it. At the end I figured it’s the ampersand problem, and here is the fix (sometimes the simplest things…)
str_replace("upgrade-plugin&plugin",'update-selected&plugins', $updateItem);
Update:
I had to use this method, because the ampersand was extracted from HTML document with preg_match. To avoid issues like this – use
html_entity_decode()
Just wanted to say that this saved me some time mate. Thanks for the post
damn, I tried everything for hours! entity decode never crossed my mind.
thanks a bunch, cheers.
This saved me some time too, many thanks!