MyBB Community Forums
PHP and \' [SOLVED] (changing wrong code) - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Community (https://community.mybb.com/forum-12.html)
+--- Forum: General Discussion (https://community.mybb.com/forum-11.html)
+--- Thread: PHP and \' [SOLVED] (changing wrong code) (/thread-55768.html)



PHP and \' [SOLVED] (changing wrong code) - Firestryke31 - 2009-08-30

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]


RE: PHP and \' - Aincalandorn - 2009-08-30

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.


RE: PHP and \' - MrD. - 2009-08-30

Why not just use stripslashes and addslashes?
http://us.php.net/manual/en/function.stripslashes.php


RE: PHP and \' - Firestryke31 - 2009-08-31

@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.


RE: PHP and \' [SOLVED] (changing wrong code) - Zash - 2009-08-31

Why do you keep adding \ before the ' in your post?


RE: PHP and \' [SOLVED] (changing wrong code) - Firestryke31 - 2009-08-31

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]


RE: PHP and \' [SOLVED] (changing wrong code) - Zash - 2009-08-31

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.