Page 1 of 1

Newbie - Quotes in HTML problem

Posted: Mon May 29, 2006 11:53 am
by HenryBourne
Hi guys, I'm sorry if this is really obvious. I've fairly recently started learning PHP, I'm reading the book "PHP and MySQL Web Development" by Welling and Thomson, and I'm trying to implement one of their examples to get some practice. It's basically a PHP template for easily adding pages to a web site so the structure remains the same, but the main content can be easily changed. A web page would, for example, contain this:

Code: Select all

<?php
	require('page.inc');
	
	$homepage = new Page();
	
	$homepage->content = "	<h3>Vic Demone’s music and live shows are an explosive celebration of melodies, beats and stories.
							</h3>
							<p>	While Vic Demone’s primary instrument is his song writing, his distinct voice and powerful performances have proven to be an undisputed force in his ability to engage with the audience.
							</p>";
			
	$homepage->Display();
?>
the "page.inc" file contains:

Code: Select all

<?php

class Page
{
	// class Page's Attributes
	public $content;
	public $title = 'Vic Demone';
	public $keywords = 'Vic Demone, music, band, Dror, Mohar';
	public $stylesheet = 'vicDemone.css';
	
	// class Page's Operations
	public function __set($name, $value)
	{
		$this->$name = $value;
	}
	
	public function Display()
	{
		echo "<html>\n<head>\n";
		$this -> DisplayHead();
		echo "</head>\n<body>\n<div id=\"main\">";
		$this -> DisplayNav();
		$this -> DisplayContent();
		$this -> displayFooter();
		echo "\n</div>\n</body>\n</html>\n";
	}
	
	public function DisplayHead()
	{
		echo '	<title>'.$this->title.'</title>
				<meta name="keywords" content="'.htmlentities($this->keywords).'" />
				<link rel="stylesheet" type="text/css" href="'.$this->stylesheet.'" />';
	}
	
	public function DisplayNav()
	{
		require('nav.php');
	}
	
	public function DisplayContent()
	{
		echo '	<div id="content">
				'.$this->content.'
				</div>';
	}
	
	public function DisplayFooter()
	{
		require('footer.php');
	}
}
	
?>
Everything works, apart from that the single quotes in the HTML that makes up the content are replaced by three strange characters when the page is loaded in a browser. If that makes sense.

I know some of the code may seem a bit basic or badly written, but I'm new! ;)
Can anyone help me out?
Thanks
Henry

Posted: Mon May 29, 2006 12:20 pm
by RobertGonzalez
It has to do with this function and what is passed to it...

Code: Select all

<?php
public function DisplayContent()
{
    echo '  <div id="content">'.$this->content.'</div>';
} 
?>
What's happening is that PHP is seeing a single quote in the content var...

Code: Select all

<?php
$this->content = "  <h3>Vic Demone’s music and live shows are an explosive celebration of melodies, beats and stories.
                                                        </h3>
                                                        <p>     While Vic Demone’s primary instrument is his song writing, his distinct voice and powerful performances have proven to be an undisputed force in his ability to engage with the audience.
                                                        </p>"; 
?>
Since the content has a single quote and the echo is wrapped in a single quote, as soon as content is parsed it stops at the first single quote it finds thinking the string is over. Try changing the function to this and see if it works a little better...

Code: Select all

<?php
public function DisplayContent()
{
    $this->content = addslashes($this->content);
    echo '  <div id="content">'.$this->content.'</div>';
} 
?>

Posted: Mon May 29, 2006 1:38 pm
by HenryBourne
Hi! That works great thankyou! :)

I had actually tried that before I posted on here. I understand the logic behind it too. However The reason it didn't work for me before is that the page.inc file must have been stored in the cache. I had to reset Safari before it had any effect.

Looks like I'll be resetting before I bother you on here again!

Thanks again.

Posted: Mon May 29, 2006 2:48 pm
by HenryBourne
Hi again! I've just come across another problem. While the text now displays correctly, if I try to insert an image (or any other HTML element that contains attributes) into the content variable, it fails to show up. This is my code for the page:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<?php
	require('page.inc');
	
	$homepage = new Page();
	
	$homepage->content = "	<h3>Listen to the sounds of Vic Demone</h3>
	<div>
	<img src='images/HolesDemoSmall.jpg' alt='Holes' />
	<p>	The latest release from Vic Demone.... Holes
	</p>
	</div>";
			
	$homepage->Display();
?>
And this is the source for the image element generated by the browser:

Code: Select all

<img src=\'images/HolesDemoSmall.jpg\' alt=\'Holes\' />
Any ideas how I can get the text to display properly, like it is now, and also have any attributes displayed correctly without the slashes?

Thanks

Posted: Mon May 29, 2006 7:00 pm
by RobertGonzalez
To be compliant with XHTML standards you should be wrapping markup in double quotes instead of single quotes. Try changing the quotes from single to double and see if that makes it work. Oh yeah, and make sure to escape the double quotes since your string wrappers are double quotes.

Code: Select all

<?php
$homepage->content = "  <h3>Listen to the sounds of Vic Demone</h3>
        <div>
        <img src=\"images/HolesDemoSmall.jpg\" alt=\"Holes\" />
        <p>     The latest release from Vic Demone.... Holes
        </p>
        </div>"; 
?>

Posted: Mon May 29, 2006 10:24 pm
by Ambush Commander
Or you could use docblocks:

Code: Select all

$homepage->content = <<<HTML
  <h3>Listen to the sounds of Vic Demone</h3>
        <div>
        <img src="images/HolesDemoSmall.jpg" alt="Holes" />
        <p>     The latest release from Vic Demone.... Holes
        </p>
        </div>
HTML;

Posted: Thu Jun 01, 2006 12:36 pm
by HenryBourne
Sorry for the late reply...

Just wanted to say thank you very much guys! You've been a great help to me! :D

Henry