一半君的总结纸

听话只听一半君

#11 How do I strip the “new-line-character” off of the end of my string variable?

When reading a DOS file where each line ends with a CR+LF pair, the ‘fgetline’ command knows to strip the LF, but fails to string the CR. This is a problem I’ve run into as well.

You can use the ‘match’ command with a regular expression to strip a CR and/or LF from the end of a text string.

The escape sequence that matches a LF is “\n“, that which matches a CR is “\r“. Armed with this:

$string = match( "^[^(\r\n)]*", $string );

What this does is matches all characters starting at the beginning of a string which is not a CR or LF character, reassigning the results to $string. This command is quick to execute, and safe to use even if there are no LF or CR characters in the string.

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: