PHP and \' [SOLVED] (changing wrong code)
#1
I\'ve decided to write my own CMS software, partially to get a better grasp of PHP, and partially because none of the other free CMSs do what I want without me having to disable and delete a bunch of cruft I don\'t need. So far, it\'s going okay, but there\'s one issue that\'s driving me up the wall: I can\'t get my PHP to remove the \ from \\'. I\'ve tried many things, but so far all have failed.

This is what I do before sending the template to the requesting code (to be eval()ed like in MyBB):
return str_replace('\\\'', '\'', $this->cache[$template]);

Here\'s what I do to store the template:
		if(function_exists($this->db["escape"]))
		{
			$escapedString = $this->db["escape"]($string, $this->connection);
		}
		else
		{
			$escapedString = addslashes($string);
			// Fix newlines
			$escapedString = preg_replace("/\n/", '\\\n', $escapedString);
		}
		return $escapedString;
$this->db[\'escape\'] is merely the name of the db driver\'s string escape function.

This is the resulting output.

It\'s driving me insane because it looks like it should work, but it doesn\'t, and so is probably something stupid. Please, make the bad \ go away! [Image: f31_sob_v3.png]
[Image: LogoO.png]
Reply
#2
You could try to change
return str_replace('\\\'', '\'', $this->cache[$template]);
to
$find = "\\\'";
$replace = "\'";
return str_replace($find, $replace, $this->cache['$template']);
Also, for your $this->cache[$template] the $template var should be in single quotes. Easier to read for humans and proper coding. (I think...)

Hope that helps.
Without death there is no life, without life the world is empty.
~Aincalandorn~
Reply
#3
Why not just use stripslashes and addslashes?
http://us.php.net/manual/en/function.stripslashes.php
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#4
@MrD.: I don\'t use stripslashes because it also removes the \ from \" which wreaks havoc when the user does eval("\$blah = \"" . $template->get("foobar") . "\";"); due to php variables (like {$title}) in the templates. It\'s the way MyBB does it, too.

@Tabbie: Wouldn\'t using $this->cache['$template'] look for an index named "$template"? I\'m looking for the index called whatever $template contains. Also, I\'ve tried using double quotes for the find and replace parameters, but I\'ll try it again.

EDIT: /facepalm. I just realized that I was editing the code that affected the templates, but what I really wanted was the code that affected something else. I've fixed it now and no longer have the \' problem.
[Image: LogoO.png]
Reply
#5
Why do you keep adding \ before the ' in your post?
Need web hosting? Try MyBBWebHost!
#1 for Running MyBB Communities
The world's only premium web host dedicated to MyBB forums.

Reply
#6
It was a joke. The problem was that my PHP code would do the same thing on my website, but it's fixed now, so I have no reason to (jokingly) use \' instead of just '. I was wondering how long before someone would comment on it [Image: f31_play_v3.png]
[Image: LogoO.png]
Reply
#7
Ah OK :p I dismissed it as a joke in the OP, but then you kept doing it and I thought there was something wrong.
Need web hosting? Try MyBBWebHost!
#1 for Running MyBB Communities
The world's only premium web host dedicated to MyBB forums.

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)