• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Help with PHP

Satelk

New Member
How do I change a variable which is on multipule lines to one line?
I already tried trim(), but it doesn't work. Please help.
PHP:
$var = "This variable
contains multipule
lines."

TO

$var = "This variable\r\ncontains multipule\r\nlines."
 
Originally posted by Palm
try \n which make its to a new line.

he means as in a form. when someone submits something with returns in it, he wants to make it one line with \n line breaks, like in a script that uses mail().

if you have the width, check this out: http://php.net/manual/en/function.mail.php , and look at the message about a wordwrap() function
 
Actually, it's for inserting a signature in a message with javascript.
And if the $signature variable is more than one line it doesn't work.
wordwrap() didn't work.
PHP:
<script language="Javascript">
function addSignature() {
  var sSignature = "<? echo $signature; ?>";
  if (sSignature.length == 0)
    alert("You can define a custom signature under Options.");
  else {
    var msg = document.compose.body;
    var ip = msg.value.indexOf("\n---- ");
    if (ip < 0) {
      msg.value += "\n\n" + sSignature + "";
    } else {
      msg.value = msg.value.substring(0, ip)
   	+ "\n\r\n" + sSignature + ""
        + msg.value.substring(ip);
    }
  }
}
</script>
 
nl2br -- Inserts HTML line breaks before all newlines in a string
Description

string nl2br (string string)


Returns string with '<br />' inserted before all newlines.

this maybe what you want - keeps the formatting

else to just remove newlines

$string=preg_replace("/\n/g","",$string);


or use

quotemeta (string) to make \n \\n so that JavaScript gets a \n literal rather than a \n !!

I think this last one maybe what you're after
 
Back
Top