Monday, August 20, 2012

PHP to Replace Last Character in a String

Seems like a trivial code but it's nice to put it here as I tend to forgot the syntax from time to time:

$line = preg_replace("/\n$/","",$line); // To replace last newline character

$line = preg_replace("/\r\n$/","",$line); // To replace last newline character in Windows environment

$line = preg_replace("/\s$/","",$line); // To replace last space in a string

Assuming your string is stored in $line.

No comments:

Post a Comment