5 Replies - 6950 Views - Last Post: 09 August 2009 - 10:17 PM Rate Topic: -----

#1 jtingato   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-February 09

parsing a php variable containing html string

Posted 07 August 2009 - 09:29 AM

I have a question on how PHP parses variable when a variable contains a string with HTML tags in it.

example:

I have queried the data base and set an obj to hold these return variables:
$query = SELECT title,  text FROM deals WHERE id = 12;

$result = mysql_query($query);
while($deal = mysql_fetch_object($result))
	{
		$deals[] = $deal;
	}



I now have an object with a lastestDeals array... which contains the deal variables.

If I loop through all these deals, I can show their titles in a table and make them links to another page which shows the complete deal.

No problem.

My problem arises when I try to add some mouseove functionality to this table. My goal is to have a mouseover event fill a <div> with the deal text and mouse out to return it to its normal state:

$Details = "Welcome to the world of deals";
foreach($deals as $key=>$deal)
		{				
			$html   .=			"\n<tr height = 30 class = 'item" . (($key % 2) + 1) . "' >";
			$html   .=				"\n<td>" .  formatStupidJoomlaDate($deal->created)."</td>";
			$html   .=				"\n<td>";
			$html   .=					  "<a href ='http://www.myWebSite.com/index.php?option=com_content&id=" . $deal->id . "&mode=browse&st=ld&Itemid=1' " .
											" onclick = 'parent.submitlink(this.href)' " .
											" onmouseover = 'showdetails(\'$deal->introtext\')' " . 
											" onmouseout  = 'showdetails(\"$details\")' > " . $deal->title . "  </a></td>";
											 
			$html   .=				"\n<td>" . $deal->hits . "</td>";
			$html   .=				getRating($deal->rating, $deal->rating_count);			
			$html   .=			"\n</tr>";		   
		}
		
		$html   .=		"</tbody>";
		$html   .=	"</table>";	
		$html   .= "</div>";
			
		return $html;



This works perfectly until the string returned from the database into $deals[x]->text contains HTML tags. At that point the PHP parser see the tags and formats the code; adding line break for each <p> tag and such. This creates a parsed file that may contain this line:

<html><head></head><body>

<a href = "myWebSite.com/deal.php?id=2" 
onclick = 'parent.submitlink(this.href)' 
onmouseover = 'showdetails("This is my deal.
<p>I hope you enjoy it</>") onmouseout(...)>




Notice that because the variable $deal->introtext got parse, it force the <p> to line break... and the final rendered html split my onmouseover statement on more than one line. Obviously a string can not be separated on two lines, so the code fails.

Is there a simple solution for this. Can I force the parser to look at that variable as a literal and not parse the tags?

Gosh, I hope this makes sense. I have included the file also for those interested. Be aware that this file is part of a Joomla environment, BUT does not run under Joomla. It runs in an Iframe and uses the Joomla database.

Thanks for any input.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: parsing a php variable containing html string

#2 gregwhitworth   User is offline

  • Tired.
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,613
  • Joined: 20-January 09

Re: parsing a php variable containing html string

Posted 07 August 2009 - 09:37 AM

First off, strings can be placed onto multiple lines using <<<

see it here: http://www.php.net/m....syntax.heredoc

Also, when you say literal wouldn't that just be placing quotes around it, but then it is seen as just a word not as a variable containing something.

As far as I know there isn't a way to make the variables get bypassed.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3800
  • View blog
  • Posts: 13,783
  • Joined: 08-August 08

Re: parsing a php variable containing html string

Posted 07 August 2009 - 09:51 AM

I think you're confusing php with html. It's html that's putting breaks with the tags. Look at this example:
<?php
$test = array("A"=>"Some text here.","B"=>"Some text\nhere.","C"=>"Some <p>text</p>here.");
foreach($test as $key=>$value)
{
	echo $value."<br>";
}
?>

Browser source html:
Some text here.<br>Some text
here.<br>Some <p>text</p>here.<br>

Browser display:
Some text here.
Some text here.
Some
text

here.

It's the way html treats the tags that matters here.
Was This Post Helpful? 0
  • +
  • -

#4 jtingato   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-February 09

Re: parsing a php variable containing html string

Posted 07 August 2009 - 12:27 PM

[quote name='CTphpnwb' date='7 Aug, 2009 - 08:51 AM' post='727903']
I think you're confusing php with html. It's html that's putting breaks with the tags. Look at this example:
[code

IC I think you're right.

Also 'Literal' was the wrong word for me to use.

consider this though;
[code]
<?php

$code = "<p>My dog is brown</p> <p>My cat is white</p>"
echo $code;
[/code]

This should render the following:
My dog is brown

My cat is white



BUT... what if I wanted it to render the var $code as a string and not HTML....as in:

<p>My dog is brown</p> <p>My cat is white</p>



If I am correct, that is what I am trying to do. Render the Html tags as chars, not process them. I am trying to do this so I can pass this html to a javascript function.

Do this make sense or am I way off?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3800
  • View blog
  • Posts: 13,783
  • Joined: 08-August 08

Re: parsing a php variable containing html string

Posted 07 August 2009 - 12:39 PM

http://www.php.net/htmlentities
<?php
$test = array("A"=>"Some text here.","B"=>"Some text\nhere.","C"=>"Some <p>text</p>here.");
foreach($test as $key=>$value)
{
	echo htmlentities($value);
	echo "<br>";
}
?>

Was This Post Helpful? 0
  • +
  • -

#6 jtingato   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-February 09

Re: parsing a php variable containing html string

Posted 09 August 2009 - 10:17 PM

uhm... Thank you. I should have known that. I feel like an idiot. Brain fart. You saved me from blowing up.
Thanks again.

View PostCTphpnwb, on 7 Aug, 2009 - 11:39 AM, said:

http://www.php.net/htmlentities
<?php
$test = array("A"=>"Some text here.","B"=>"Some text\nhere.","C"=>"Some <p>text</p>here.");
foreach($test as $key=>$value)
{
	echo htmlentities($value);
	echo "<br>";
}
?>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1