Jump to content

Remove 'New Line' (NL) from a STRING


AndieB

Recommended Posts

Hi,

 

if I fetched some data into a string from a database, how do I remove the NL (new line) in the string??

Could be a case where I also get a POST from a FORM treated by PHP.

 

Thankful for any kind of help!

 

Sincerely,

Andreas

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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" );

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.