AndieB Posted June 30, 2009 Share Posted June 30, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/ Share on other sites More sharing options...
.josh Posted June 30, 2009 Share Posted June 30, 2009 trim Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866639 Share on other sites More sharing options...
phant0m Posted June 30, 2009 Share Posted June 30, 2009 str_replace("\", "", $string) trim just removes whitespace at either end of a string Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866641 Share on other sites More sharing options...
.josh Posted June 30, 2009 Share Posted June 30, 2009 Really? So I guess the manual entry that I coincidentally supplied a link to must be lying. Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866647 Share on other sites More sharing options...
runnerjp Posted June 30, 2009 Share Posted June 30, 2009 lol crayon Violent is correct :) function trim_value(&$value) { $value = trim($value); } that would do job and its what i use! Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866687 Share on other sites More sharing options...
.josh Posted June 30, 2009 Share Posted June 30, 2009 I hope that function is being used with something like array_walk because if not, I don't really see why you can't just use trim() directly... Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866688 Share on other sites More sharing options...
gizmola Posted June 30, 2009 Share Posted June 30, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866692 Share on other sites More sharing options...
.josh Posted June 30, 2009 Share Posted June 30, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866701 Share on other sites More sharing options...
gizmola Posted June 30, 2009 Share Posted June 30, 2009 You're misreading the manual CV. What it means is that it will do the trim action on all those characters, unless you specify the 2nd parameter, which will then act only on the characters you specified. Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866705 Share on other sites More sharing options...
gizmola Posted June 30, 2009 Share Posted June 30, 2009 I agree about the ambiguity -- let's all get a beer! Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866709 Share on other sites More sharing options...
.josh Posted June 30, 2009 Share Posted June 30, 2009 right. So if I do: $string = "blah\n"; $string = trim($string); that \n will be removed. Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-866710 Share on other sites More sharing options...
deviant_two Posted July 2, 2009 Share Posted July 2, 2009 try this <?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" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-868055 Share on other sites More sharing options...
deviant_two Posted July 2, 2009 Share Posted July 2, 2009 argh sorry : <?php ##### APPLYING THE FUNCTION $str = "par 1 par 2 par 3 "; header("Content-type: text/plain"); print ( "(" . string_rn($str) . ")\n" ); print ( "(" . string_rn($str , "<=>" ) . ")\n" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/164288-remove-new-line-nl-from-a-string/#findComment-868057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.