Help with ampersands in strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Help with ampersands in strings

Post by L4E_WakaMol-King »

I'm using the following script to generate a javascript that can be used to put a blog on a webpage. It generates a table with each post's title, author, etc.

The problem that I am having is that any time an "&" appears in a post, the $content string doesn't show up, and the post appears blank.

I've tried all of the following, to no avail:
$content = str_replace("&", "", $content);
$content = str_replace("&", "&", $content);
$content = str_replace("&", "", $content);
$content = str_replace("amp;", "", $content);

Code: Select all

<?php

$insideitem = false;
$tag = "";
$title = "";
$author = "";
$date = "";
$month = "";
$day = "";
$year = "";
$content = "";
$loopnum = 0;

function startElement($parser, $name, $attrs) {
	global $insideitem, $tag, $title, $author, $date, $content;
	if ($insideitem) {
		$tag = $name;
	} elseif ($name == "ENTRY") {
		$insideitem = true;
	}
}

function endElement($parser, $name) {
	global $insideitem, $tag, $title, $author, $date, $month, $day, $year, $content, $loopnum;
	if ($name == "ENTRY") {

 	$title = str_replace("'", "\'", $title);
 	$content = str_replace("\n", "", $content);
	$content = str_replace("'", "\'", $content);
	$content = str_replace("[url]", "<a href=\'", $content);
	$content = str_replace("[/url/]", "\'>", $content);
	$content = str_replace("[/url]", "</a>", $content);

		echo 'document.write(\'<tr>\');
';

		printf('document.write(\'<td class="windowbg" valign="top"><img id="p%s" src="http://l4eclan.com/images/blogavatars/%s.gif" style="display: none" /></td>\');', $loopnum, trim($author));
		echo '
document.write(\'<td class="windowbg2">\');
';
		printf('document.write(\'<b><a onclick="display(%s)" style="color: #0099FF; cursor: pointer">%s</a></b>\');', $loopnum, trim($title));
		printf('document.write(\'<p id="t%s" style="display: none"><br /><br />%s</p></td>\');', $loopnum, trim($content));
		printf('document.write(\'<td colspan="2" class="windowbg" valign="top"><center><b>%s<b></center></td>\');', trim($author));
		printf('document.write(\'<td class="windowbg2" valign="top"><center>%s %s, %s</center></td>\');', $month, $day, $year);
		echo '
document.write(\'</tr>\');
';

		$title = "";
		$author = "";
		$date = "";
		$content = "";
		$loopnum = $loopnum + 1;
		$insideitem = false;
	}
}

function characterData($parser, $data) {
	global $insideitem, $tag, $title, $author, $date, $month, $day, $year, $content;
	if ($insideitem) {
	switch ($tag) {
		case "TITLE":
		$title .= $data;
		break;
		case "NAME":
		$author .= $data;
		break;
		case "CREATED":
		$date .= $data;
		break;
		case "DIV":
		$content .= $data;
		break;
		case "BR":
		$content .= "<br />" . $data;
 		break; }

	switch (substr($date,5,2)) {
		case "01":
		$month = "January";
		case "02":
		$month = "February";
		case "03":
		$month = "March";
		case "04":
		$month = "April";
		case "05":
		$month = "May";
		case "06":
		$month = "June";
		case "07":
		$month = "July";
		case "08":
		$month = "August";
		case "09":
		$month = "Semptember";
		case "10":
		$month = "October";
		case "11":
		$month = "November";
		case "12":
		$month = "December";
		break; }

	$day = (substr($date,8,2));
	$year = (substr($date,0,4));

	}
}

echo '
document.write(\'<table id="forCat" style="display: none;">\');
document.write(\'<tr>\');
document.write(\'<td class="windowbg" colspan="4"><b><font size="2">Recent Posts on the L4E Blog</font></b></td>\');
document.write(\'<td class="windowbg" align="center">[<a href="http://www.blogger.com/post-create.g?blogID=19669778" target="_blank">New Post</a>] [<a href="http://l4ewakamolking.proboards39.com/index.cgi?action=display&board=L4Earchives&thread=1150270549&page=1"><font style="color: #0099FF">?</font></a>]</td>\');
document.write(\'</tr>\');
';

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://l4eblog.blogspot.com/atom.xml","r")
	or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
	xml_parse($xml_parser, $data, feof($fp))
		or die(sprintf("XML error: %s at line %d",
			xml_error_string(xml_get_error_code($xml_parser)),
			xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

echo '
document.write(\'</table>\'); document.write(\'<script type="text/javascript">function display(num) { if (document.getElementById("p" + num).style.display == "none") { document.getElementById("p" + num).style.display="inline"; document.getElementById("t" + num).style.display="inline"; } else { document.getElementById("p" + num).style.display="none"; document.getElementById("t" + num).style.display="none"; } }</script>\');
';

?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think I ran into this problem once before. Correct me if I'm wrong, but are you taking a string in PHP, escaping it then using Javascript to insert that escaped string into the page? If that's the case, I believe the problem is that when you inject the string from PHP into Javascript, that special character code (in your case &) is being interpreted, and you're back to square one.

I'm trying to remember how I fixed it - I think if you replace all occurrences of '&' with '&amp;' it might work.


Just curious why you're writing a PHP script to write Javascript to put content on a page? Why not just use the PHP file to place the content & forget about the Javascript?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

Well, I'm adding this to a forum which is hosted at Proboards.com, so I can't modify the source code myself.

I tried both of these and no luck:
$content = str_replace("&", "&amp;", $content);
$content = str_replace("&", "&amp;", $content);

I think the error is happening in the PHP, not the JS. When I go to the file (http://l4eclan.com/headfoot/forumblog.js), there is nothing in the content section of the post. There is if you visit it now, because I took all the &'s out of the posts, but if I were to post something with a &, the whole script would go haywire.
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

*bumpage*
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you assign the ampersand to a JavaScript string then concatenate that into the final displayed string? Don't know if it'll achieve what you want, but I have seen others do similar things in other cases.
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

That's a really good idea. I will give that shot. Thanks!
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

Well, actually I can't try that :?.

The problem is that none of the $content string gets written to the file when the php code is running... so I can't do anything with it once it gets to the javascript.

I've tried all of these:
$content = str_replace("&", "a", $content);
$content = str_replace("&", "a", $content);
$content = str_replace("&amp;", "a", $content);

No matter how I do it, none of the content part of the post is rendered.

I uploaded a test version of the file here, if anyone wants to take a look.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you tried wrapping all that mess in <script language="JavaScript"> javascript </script> tags? Just a thought, but try that.
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

If I do that, it can't be read as a script. (I did try it out, just to be sure)

I'm 99% sure the problem is not with the javascript. The script is perfectly valid, accoding the to JS interpreter... the problem is that the php is not working. When it's supposed to write the $content string, it does nothing if the $content string has any &'s in it. If there aren't any &'s in it, it writes the string just fine.

Go here and view the source:
http://www.l4eclan.com/headfoot/forumblog.php

And if it helps any, here is the XML file it is parsing:
http://l4eblog.blogspot.com/atom.xml
(probably want to view the source on that one too)
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

try using array_flip on the content string using the translation_table in your xml praser because some of the strings are encode and some are not. If you flip them then only the encoded parts will be converted back to their ISO form! This way you standardize the data to follow one encoding rule that the PHP XML parser understands, which also correct encoding errors in your JavaScript, because before you start using string replace, you should be sure the strings contain the encoding that the string replace functions you are using were made for!


printf!
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

printf wrote:try using array_flip on the content string using the translation_table in your xml praser
Sorry, I'm kind of new at this... could you explain in a bit more detail? I get the gist of what you are saying, but I'm not sure how to put it into code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your source shows a bunch of document.write calls. Your browser has no way of knowing what is happening with that. Regardless of what you are using to put content into the document.write calls, without knowing what to do with it your browser will show it as text.
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

The output of this php script is not meant to be displayed on a browser directly. It's a remote javascript to be called from another website. I'm using it to put the posts from a blog onto a forum.

You need to view the "source code" of the php script to see what I was saying about the $content string never making it into the output file. It does make it into the output file if the $content string has no &'s in it. What I mean is go to that link and do View > Page Source (FF) or Edit > View Source (IE) and see the output of the php file (I'm making the destinction because the output of the php file is actually the source code of a javascript).

To use psudocode, the php file is supposed to generate something like this (and it does):

write: <title>Post Number Three</title>
write: <author>Stephen</author>
write: <content>This post has a link in it!</content>
write: <title>Post Number Two</title>
write: <author>Bob</author>
write: <content>PHP is fun.</content>
write: <title>Post Number One</title>
write: <author>Anthony</author>
write: <content>I heart L4E.</content>

But, if anyone puts a & anywhere in the content of a post, the php script doesn't work properly. Say the content of Stephen's post is "This post has a link in it & I love links!" The php script generates:

write: <title>Post Number Three</title>
write: <author>Stephen</author>
write: <content></content>
write: <title>Post Number Two</title>
write: <author>Bob</author>
write: <content>PHP is fun.</content>
write: <title>Post Number One</title>
write: <author>Anthony</author>
write: <content>I heart L4E.</content>

Hope that makes more sense.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Total shot in the dark, but can you escape it with a slash (ie \&)?
L4E_WakaMol-King
Forum Commoner
Posts: 26
Joined: Wed Jun 14, 2006 10:54 am

Post by L4E_WakaMol-King »

I can't seem to do anything with it... not event get rid of it. :?

You mean something like this, right?
$content = str_replace("&", "\&", $content);

But I can't even seem to do this:
$content = str_replace("&", "", $content);

:(
Post Reply