Escape characters in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Escape characters in php

Post by kinabaloo »

In many languages '\n' means the line break character

how to write it in php?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Escape characters in php

Post by VladSun »

The same way - just use double quotes, not single ones.

Code: Select all

echo "A new line character \n is put here";
There are 10 types of people in this world, those who understand binary and those who don't
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post 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 ?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Escape characters in php

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post 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 />";
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Escape characters in php

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post 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.
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post 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 ...
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post 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.
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post 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?
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post 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 :(
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post by kinabaloo »

If I write this:

$order=array("\n");

I get a '; expected' error - why is that?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Escape characters in php

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

Post by kinabaloo »

$aaa = nl2br($aaa);

Gives a syntax error :(

why is that ?
kinabaloo
Forum Commoner
Posts: 35
Joined: Tue Jul 28, 2009 11:20 pm

Re: Escape characters in php

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