Page 1 of 2

Escape characters in php

Posted: Wed Jul 29, 2009 6:42 am
by kinabaloo
In many languages '\n' means the line break character

how to write it in php?

Re: Escape characters in php

Posted: Wed Jul 29, 2009 6:46 am
by VladSun
The same way - just use double quotes, not single ones.

Code: Select all

echo "A new line character \n is put here";

Re: Escape characters in php

Posted: Wed Jul 29, 2009 6:57 am
by kinabaloo
VladSun wrote:The same way - just use double quotes, not single ones.

Code: Select all

echo "A new line character \n is put here";
double quotes in str_replace("\n","<br>", $a) too ?

Re: Escape characters in php

Posted: Wed Jul 29, 2009 7:03 am
by VladSun

Re: Escape characters in php

Posted: Wed Jul 29, 2009 7:10 am
by kinabaloo
VladSun wrote:The same way - just use double quotes, not single ones.

Code: Select all

echo "A new line character \n is put here";
It works! Thanks :)

Lastly, this does n't append as epected. What's wrong?

$aaa = $aaa . "<br />";

Re: Escape characters in php

Posted: Wed Jul 29, 2009 7:14 am
by VladSun
kinabaloo wrote:Lastly, this does n't append as epected. What's wrong?

$aaa = $aaa . "<br />";
Nothing wrong. I don't know what "doesn't append as expected" means ;)

Post more code, please.

Also, you may use a short form for this one:

Code: Select all

$aaa .= "<br />";
PS:
Please, use [ php ] BB tags instead of [ code ] BB tags when posting PHP code.

Re: Escape characters in php

Posted: Wed Jul 29, 2009 7:22 am
by kinabaloo
VladSun wrote:
kinabaloo wrote:Lastly, this does n't append as epected. What's wrong?

$aaa = $aaa . "<br />";
Nothing wrong. I don't know what "doesn't append as expected" means ;)

Post more code, please.

Also, you may use a short form for this one:

Code: Select all

$aaa .= "<br />";
PS:
Please, use [ php ] BB tags instead of [ code ] BB tags when posting PHP code.
Ok, Thanks.

Re: Escape characters in php

Posted: Wed Jul 29, 2009 7:27 am
by kinabaloo
The thing is, the str_replace with double quotes only works in some places. I think that when inside a function is when it doesn't work.

In this case it is does not:

Code: Select all

function onGetContent( $editor ) {
 
$aaa = "document.getElementById( '$editor' ).value;\n";
 
$aaa = str_replace("\n", "x", $aaa);
 
return $aaa;
 
}
 
If I replace "\n" with say "k" no problem; get an exception when it with "\n"

Thanks for any ideas why ...

Re: Escape characters in php

Posted: Wed Jul 29, 2009 8:01 am
by kinabaloo
I have been trying out

$aaa = nl2br($aaa);

but i get a php error with this (this line is inside a function).

nl2br($aaa);

is ok, though I obviously dont get the result.

How to do the assignment properly?

However, to be honest I'd prefer to get

str_replace("\n", "x", $aaa);

working.

Re: Escape characters in php

Posted: Wed Jul 29, 2009 8:33 am
by kinabaloo
Ah, I think the problem might be this:

that i have to return an Object rather than a String.

Does that make sense? How to change my object into a string for the relacements, then the string back into an oject? Perhaps the object is an array of strings?

Code: Select all

 
 
function onGetContent( $editor ) {
 
     $aaa = "document.getElementById( '$editor' ).value;\n";
 
     $aaa = str_replace("\n", '#', $aaa);
 
//or something like this required?
 
     $order=array("\n");
     $replace = '#';
     $newaaa[] = str_replace($order, $replace, $aaa);
     $aaa='';
        foreach ($newaaa as $result) {
            //if (trim($result)) 
            {
                $aaa .= $result;
            }
        }
 
        return $aaa;
    }
 
I always get an Object Required error from php. Is $aaa not proper object?

Re: Escape characters in php

Posted: Wed Jul 29, 2009 9:00 am
by kinabaloo
My best guess at what it should be is

Code: Select all

 
function onGetContent( $editor ) { 
   $aaa[] = "document.getElementById( '$editor' ).value;\n"; 
 
   $order=array("\n"); 
   $replace = '#'; 
   $newaaa[] = str_replace($order, $replace, $aaa); 
 
   return $newaaa; 
}
 
But same Object Expected error :(

Re: Escape characters in php

Posted: Wed Jul 29, 2009 9:17 am
by kinabaloo
If I write this:

$order=array("\n");

I get a '; expected' error - why is that?

Re: Escape characters in php

Posted: Wed Jul 29, 2009 9:19 am
by VladSun
kinabaloo wrote:The thing is, the str_replace with double quotes only works in some places. I think that when inside a function is when it doesn't work.

In this case it is does not:

Code: Select all

function onGetContent( $editor ) {
 
$aaa = "document.getElementById( '$editor' ).value;\n";
 
$aaa = str_replace("\n", "x", $aaa);
 
return $aaa;
 
}
 

If I replace "\n" with say "k" no problem; get an exception when it with "\n"

Thanks for any ideas why ...
Using this exact function (please, show its usage also), please tell:

What does the Excpetion say? What is the error message?

Re: Escape characters in php

Posted: Wed Jul 29, 2009 9:24 am
by kinabaloo
$aaa = nl2br($aaa);

Gives a syntax error :(

why is that ?

Re: Escape characters in php

Posted: Wed Jul 29, 2009 9:25 am
by kinabaloo
VladSun wrote:
kinabaloo wrote:The thing is, the str_replace with double quotes only works in some places. I think that when inside a function is when it doesn't work.

In this case it is does not:

Code: Select all

function onGetContent( $editor ) {
 
$aaa = "document.getElementById( '$editor' ).value;\n";
 
$aaa = str_replace("\n", "x", $aaa);
 
return $aaa;
 
}
 

If I replace "\n" with say "k" no problem; get an exception when it with "\n"

Thanks for any ideas why ...
Using this exact function (please, show its usage also), please tell:

What does the Excpetion say? What is the error message?
'Object Expected'

The function that calls it expects an array.