Page 1 of 1

Unexpected character in input: '\' (ASCII=92) state=1

Posted: Sun Nov 04, 2007 11:47 pm
by Mr Tech
I'm trying to addslashes() to some text but it returns this error
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /var/flexshare/shares/php/development/cms/cms/modules/pages/functions-pages.php(158) : eval()'d code on line 1

Parse error: parse error, unexpected $ in /var/flexshare/shares/php/development/cms/cms/modules/pages/functions-pages.php(158) : eval()'d code on line 1
On line 158 is the following code:

Code: Select all

$template = addslashes($template);
I also tried the following

Code: Select all

$template = str_replace("'", "'", $template);
$template = str_replace("\"", """, $template);
And got this error
Parse error: parse error, unexpected '&' in /var/flexshare/shares/php/development/cms/cms/modules/pages/functions-pages.php(158) : eval()'d code on line 317
Is there something in the code above that's causing the error or is there something else.

Posted: Mon Nov 05, 2007 3:59 am
by aaronhall
addslashes() and eval() are two of the most evil things in PHP... there's rarely a good reason to use either of them. I assume $template is being eval()ed? var_dump($template) before the eval() line and post it here, but if you can explain what you're trying to accomplish, there's almost certainly a better way to do it.

Posted: Mon Nov 05, 2007 5:15 pm
by Mr Tech
aaronhall wrote:addslashes() and eval() are two of the most evil things in PHP... there's rarely a good reason to use either of them.
Great! And I just happen to be using them both! 8O

I'm using eval() because this is a CMS that includes a template file. Sometimes I need to put PHP code in the template but the PHP code does not work unless I use eval()... It pastes it as plain text otherwise...

I currently use the following to include the template. If there was a better way, maybe I could remove the eval() altogether?

Code: Select all

function load_template($filename)
{
	if (file_exists($filename)) {
		$fd = @fopen($filename, 'r');
		$filecontents = @fread ($fd, filesize($filename));
		@fclose ($fd);
	} else {
		$filecontents = "<p align="left"><font color="red">Template file <b>$filename</b> not found.</font></p>";
	}
	return $filecontents;
}
I'm using addslashes() because when you edit the page in the CMS admin, it uses AJAX to save and reload the content. I have to put the code into a javascript function to reload the content...

e.g:

Code: Select all

window.parent.updateTemplate("the code goes here and needs to have slashes added to it");
All that var_dump($template) returns is the following:
string(18303) "
Thanks for your help!!!

Posted: Mon Nov 05, 2007 11:36 pm
by Mr Tech
I think it's all making sense now...

Obviously eval() does not like it when you use addslashes() before hand because it stuffs up any PHP code... And I can't run eval() before I run addslashes() (correct me if I'm wrong...) because eval() prints onto the page straight away...

I know this all probably made sense to you before but I only just got it.

I think the only way to really solve this is change the way I do my templating... I still need to be able to run PHP code within the template though... Any suggestions?

Are there any template scripts out there that have a way around this? Basically I need to be able to run the PHP code before I add the addslashes(). I know vbulletin allows it to run PHP code in it's templates. How does it do it without eval()?

What do you reckon?