dumb question

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

webmeister
Forum Newbie
Posts: 8
Joined: Thu Aug 02, 2007 4:11 pm

dumb question

Post by webmeister »

I am trying to construct a string such as

$str = "&x=7&y=12";

I need to send these characters out literally but php inserts a '#' between the & and the x.

if I output, it gives me this:

&#x=7&y=12

how do I fix this?

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Show code please.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Moved to PHP Code.
webmeister
Forum Newbie
Posts: 8
Joined: Thu Aug 02, 2007 4:11 pm

Post by webmeister »

Jcart wrote:Show code please.

I thought I already did.

If I make a string such as this:

$str = "&x=7&y=12";


and I echo it back ie:

echo $str;


The output I get is:

&#x=7&y=12


Php is putting a '#' in between the '&' and the 'x'


I am new to php; my experience is in C
Is there some kind of an escape sequence to fix this or is it a bug in php?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Perhaps I can clarify what Jcart was asking...

Post the code that you are actually running, not the one line that you are having problems with. There could be any number of things taking place. Unless of course your entire PHP app is

Code: Select all

<?php
$str = "&x=7&y=12"; 
echo $str;
?>
in which case I really couldn't tell you what is up.

To better help us help you though, please run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$flags['Config file'] = get_cfg_var('cfg_file_path');
if (empty($flags['Config file']))
{
	$flags['Config file'] = '-';
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
webmeister
Forum Newbie
Posts: 8
Joined: Thu Aug 02, 2007 4:11 pm

Post by webmeister »

Everah wrote:Perhaps I can clarify what Jcart was asking...

Post the code that you are actually running, not the one line that you are having problems with. There could be any number of things taking place. Unless of course your entire PHP app is

Code: Select all

<?php
$str = "&x=7&y=12"; 
echo $str;
?>
in which case I really couldn't tell you what is up.

To better help us help you though, please run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$flags['Config file'] = get_cfg_var('cfg_file_path');
if (empty($flags['Config file']))
{
	$flags['Config file'] = '-';
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
I just left a sniplet because I think that should be enough.


I get this from your source:

PHP Version: 4.4.7
PHP OS: Linux
Error Reporting: 2039 ((E_ALL | E_STRICT) ^ E_STRICT ^ E_NOTICE))
Register Globals: Off
Short Tags: On
Display Errors: On
Magic Quotes GPC: On
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Config file: /usr/local/Zend/etc/php.ini
Loaded Extensions:
zip xmlrpc xml tokenizer
standard sockets session pspell
posix overload mysql mhash
mcrypt mbstring gettext gd
ftp exif domxml curl
ctype calendar bcmath zlib
pcre openssl apache ffmpeg
suhosin Zend Optimizer
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 »

webmeister wrote:I just left a sniplet because I think that should be enough.
Well it's not - that's why we've asked for more.

Try dropping the double quotes for single quotes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
webmeister
Forum Newbie
Posts: 8
Joined: Thu Aug 02, 2007 4:11 pm

Post by webmeister »

pickle wrote:
webmeister wrote:I just left a sniplet because I think that should be enough.
Well it's not - that's why we've asked for more.

Try dropping the double quotes for single quotes.
I found out that the problem isn't php; it is my netscape browser! If I use internet explorer, it comes back correct; if I use netscape it inserts a # between the & and the x; I don't know why.....do you?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

webmeister wrote:I just left a sniplet because I think that should be enough.
Little snippets are never enough. That's why we ask for more. ;)

Try this:

Code: Select all

<?php
echo '&x=7&y=12';
?>
And post back what is echo'ed.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

webmeister wrote:I found out that the problem isn't php; it is my netscape browser! If I use internet explorer, it comes back correct; if I use netscape it inserts a # between the & and the x; I don't know why.....do you?
What version of the Netscape browsers are you using? What is the character set encoding on the markup that is being sent to the browser?
webmeister
Forum Newbie
Posts: 8
Joined: Thu Aug 02, 2007 4:11 pm

Post by webmeister »

Everah wrote:
webmeister wrote:I just left a sniplet because I think that should be enough.
Little snippets are never enough. That's why we ask for more. ;)

Try this:

Code: Select all

<?php
echo '&x=7&y=12';
?>
And post back what is echo'ed.

netscape 7.2 using view source:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
&#x=7&y=12
</body>
</html>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Now t try this and see what comes of it:

Code: Select all

<?php
echo '&x=7&y=12';
echo '<br />';
echo urlencode('&x=7&y=12');
?>
webmeister
Forum Newbie
Posts: 8
Joined: Thu Aug 02, 2007 4:11 pm

Post by webmeister »

I also used a network sniffer so I know the php is sending what it's supposed to. I guess netscape inserts the '#' between the '&' and a 'x'; I just don't understand why. Must be a netscape thing!
webmeister
Forum Newbie
Posts: 8
Joined: Thu Aug 02, 2007 4:11 pm

Post by webmeister »

Everah wrote:Now t try this and see what comes of it:

Code: Select all

<?php
echo '&x=7&y=12';
echo '<br />';
echo urlencode('&x=7&y=12');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
&x=7&y=12<br />%26x%3D7%26y%3D12
</body>
</html>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

webmeister wrote:I also used a network sniffer so I know the php is sending what it's supposed to. I guess netscape inserts the '#' between the '&' and a 'x'; I just don't understand why. Must be a netscape thing!
Maybe because & is special symbol - it marks the begining of HTML entity. And a hex value HTML entity looks like:
&#x31; (that is for '1')
Last edited by VladSun on Thu Aug 02, 2007 6:27 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply