Different parsing behavior when saving with different editor
the title describes my problem. i parse a text file with php. this file contains an url to a google calendar feed
http://www.google.com/calendar/feeds/example%40googlemail.com/public/full
i access the feed information like this
$doc = new DOMDocument();
$doc->load( $feed );
when i save this file with the mac textedit, then everthing is fine. but when i save it with vim on linux or mac, then the url which gets loaded is
http://www.googl开发者_运维知识库e.com/calendar/feeds/example%2540googlemail.com/public/full%0A
note that the percentage sign gets transformed to: % -> %25 and a lineending to %0A
with this url i get an error when accessing the feed information, because the url is wrong. what is the problem with saving a text file with vim? encoding?
regards, peter
%0A
is the encoding of a linefeed character. In other words, your line endings are different in different editors (carriage return for TextEdit, linefeed for vim).
If you want vim to write out CR line endings, use the following command:
:set fileformat mac
There should be a parameter "auto_detect_line_endings" in your php.ini which is normally set to "Off". Changing it, should solve the problem in place.
If this is not feasible for you, then you could clean-up any unwanted characters after loading you input doc. The php function str_replace() does this a bit faster than a regular expression, so I'd recommend it for your case:
$mystring = str_replace(chr(10), "", $mystring); //remove carriage returns
$mystring = str_replace(chr(13), "", $mystring); //remove carriage returns
I'm sure there is a more efficient solution somewhere in the DOM/LIBXML library, but I haven't investigated it.
精彩评论