Jump to content

Remove 'New Line' (NL) from a STRING


AndieB

Recommended Posts

Really? So I guess the manual entry that I coincidentally supplied a link to must be lying.

 

Guys? trim only removes whitespace characters from beginning and end of strings. Phant0m is correct.

 

$str = "This is line 1.\nThis is line 2.\nThis is line3.\n";
$str = trim($str);
echo nl2br($str);

from the manual:

Description

string trim ( string $str [, string $charlist ] )

 

This function returns a string with whitespace stripped from the beginning and end of str . Without the second parameter, trim() will strip these characters:

 

    * " " (ASCII 32 (0x20)), an ordinary space.

    * "\t" (ASCII 9 (0x09)), a tab.

    * "\n" (ASCII 10 (0x0A)), a new line (line feed).

    * "\r" (ASCII 13 (0x0D)), a carriage return.

    * "\0" (ASCII 0 (0x00)), the NUL-byte.

    * "\x0B" (ASCII 11 (0x0B)), a vertical tab.

 

I will concede that that doesn't remove new lines from in the middle of the string...OP is somewhat ambiguous on that count though...

try this  8)

 

<?php

##### DECLARE THE FUNCTION

if (!function_exists("string_rn")){ #> this F will avoid declaring this F twice (usual programmer use include() rather than include_once() method )
function string_rn($string){	//func_get_arg(1);	//func_num_args(); # optinal (you can add your additional parameter here just dont forget 'func_get_arg($y0Ur_nUmBEr)' )
# this F will convert \n (Unix Style) (ASCII 10 (0x0A)) , \r (Mac Style) , \r\n (Win Style)

$string=func_get_arg(0);
$to="\n";
if (func_num_args()>1){
	$to=func_get_arg(1);
}
# this '$dump' var it just for creating new dump var
$dump=
	mktime
		(
		date ("H")
		,date ("i")
		,date ("s")
		,date ("m")
		,date ("d")
		,date ("y")
		)
	.date ("Y-m-d")
	.date ("H:i:s")
	.tempnam
	;
if (function_exists("sha1")){$dump=sha1($dump);} # sha1 availabe in (PHP 4 >= 4.3.0, PHP 5) #

$willReplace=array ();
$willReplace["src"]=array();$willReplace["des"]="";
	$willReplace["src"][]="\r\n";# \r\n (Win Style)
	$willReplace["src"][]="\r";# \r (Mac Style)
	$willReplace["src"][]="\n";# \n (Unix Style)
$willReplace["des"]=$dump;

if (function_exists("str_ireplace")){
	$string=str_ireplace( $willReplace["src"] , $willReplace["des"] , $string ); # Only available in PHP 5 #
}else{
	$string=str_replace ( $willReplace["src"] , $willReplace["des"] , $string ); # Available in PHP 4,5,6 #
}

if (function_exists("str_ireplace")){
	$string=str_ireplace( $willReplace["des"] , "$to" , $string );
}else{
	$string=str_replace ( $willReplace["des"] , "$to" , $string );
}

return $string;
unset( $string , $willReplace , $dump) ;
};};

?>
<?php

##### APPLYING THE FUNCTION 

$str = " should i use unset() function inside myOwn functions ?
I really dont have a idea about this
really?
Oh thank u for replying
";
header("Content-type: text/plain");
print ( "(" .  string_rn($string) . ")\n" );
print ( "(" .  string_rn($string, "<=>" ) . ")\n" );

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.