php formatting new lines and spacing

518 Views Asked by At

This is a little complicated to explain but I'll try my hardest, I'm trying to create a tool to edit channel descriptions for TeamSpeak 3, to do this you use a feature called channeledit.

example usage: channeledit channel_description=My\sDescription

Presumably \s = space \n = newline, is there any possible way from using a textarea to php script to have it output the line as:

My\sDescription\nWelcome\sto\smy\sServer

Rather than appearing as: My Description Welcome to my Server

If there is spacing or line breaks, this kills the command and stops it working. Can anyone give me a bit of help here?

Code for this is:

$name = "Test Test Test test test";

$ts3_VirtualServer->execute("channeledit cid=" . $current_cid . " channel_description=" . $name);

3

There are 3 best solutions below

3
On

$name=STR_replace(" ","/s",$name) $name= str_replace("\n", '\n', $name);

5
On
$name = "Test Test
Test test test";
(string)$newname = str_replace(' ', '\\s', $name);
$newname = urldecode(str_replace('%0A', "\\n", urlencode($newname)));

You need to escape (\) the backslash (\). %0A 's are easier to find. My output:

Test\sTest\nTest\stest\stest

0
On

If you want to replace spaces and carriage returns with literal \n and \s. I would do the following:

$name = urldecode(str_replace("%0D%0A","\\n",str_replace("+","\\s",urlencode($name))));