简体   繁体   中英

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.google.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM