Search code examples
phpstringvar-dump

php string not usable in method file_get_contents($var)


I have a method that scrapes data from a url and returns that as a string variable. Currently the method is working if i put in my own url, but when i insert a generated url it doesnt work.

e.g.

The following string is working if I insert it into a variable, and pass it:

http://www.rijkswaterstaat.nl/apps/geoservices/rwsnl/awd.php?mode=html&projecttype=windsnelheden_en_windstoten&category=1&loc=ZBWI&net=LMW

But this string is being generated by another source. The result of my attempt to fetch it is (var_dump()):

string(154) "http://www.rijkswaterstaat.nl/apps/geoservices/rwsnl/awd.php?mode=html&projecttype=windsnelheden_en_windstoten&category=1&loc=ZBWI&net=LMW"

The string is only 138 characters, however it prints string(158). I think this has something to do with the fact it is not working, but i'm not even sure...

Does anyone have any idea how to clean this up? I have found other questions with the question why var_dump() is showing another value then the length of the string, and that had something to do with unvisible characters, but no real solution is given anywhere.

Thx


Solution

  • 154-138 = 16

    You have 4 & in the string

    & HTML encoded is &

    So your string seems to be HTML encoded - in the browser you don't see the encoding unless you "View Source".

    You can use html_entity_decode() to decode the string or, if possible, make sure that you get a string that is not encoded for HTML output in the first place.