By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,106 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,106 IT Pros & Developers. It's quick & easy.

echo a string that contain <? and <?php

P: n/a
Can anybody help me with this problem:

How can I echo a string that contain <? and <?php?

For example,

$str="test <? and <?php echo";
echo $str;
What I get is: test instead of: test <? and <?php echo
Thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 20 Jul 2004 07:15:13 -0700, haoren hath writ:
Can anybody help me with this problem:

How can I echo a string that contain <? and <?php?

For example,

$str="test <? and <?php echo";
$str="test &lt;? and &lt;?php echo";
echo $str;
What I get is: test instead of: test <? and <?php echo
Thanks.

Jul 17 '05 #2

P: n/a
.oO(haoren)
How can I echo a string that contain <? and <?php?

For example,

$str="test <? and <?php echo";
echo $str;


echo htmlspecialchars($str);

http://www.php.net/htmlspecialchars

Micha
Jul 17 '05 #3

P: n/a
haoren wrote:
Can anybody help me with this problem:

How can I echo a string that contain <? and <?php?

For example,

$str="test <? and <?php echo";
echo $str;

What I get is: test instead of: test <? and <?php echo

Thanks.


Just use single quotes:
<?php
$string = 'test <? and <?php echo';
echo $string;
?>
Jul 17 '05 #4

P: n/a
On Tue, 20 Jul 2004 07:15:13 -0700, haoren wrote:
Can anybody help me with this problem:

How can I echo a string that contain <? and <?php?

For example,

$str="test <? and <?php echo";
echo $str;
What I get is: test instead of: test <? and <?php echo
Thanks.

You should _never_ be echoing a < or > char to the browser, this should
_always_ be &lt; or &gt;.
echo htmlentities($str);
would work in your instance for example.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #5

P: n/a
On Tue, 20 Jul 2004 15:24:59 GMT
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote:
On Tue, 20 Jul 2004 07:15:13 -0700, haoren wrote:
[SNIP]
You should _never_ be echoing a < or > char to the browser, this
should_always_ be &lt; or &gt;.


That's interesting... How do you output HTML from PHP then? :-p

What he's trying to say is that if you want a < or a > to show up in
your browser, you should use &lt; and &gt;, since < and > are the chars
used to delimit HTML tags, so the browser will think that the < or > you
just echo'ed out is part of the HTML.
So, again... If you want < and > to print in e.g. a text, use 'echo
"&lt; and &gt;";'

Madsen

--
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA/UhClNHJe/JASHcRAuwPAJ4o6pf0fZ5cP6lTfzCTx1oPTlo0lACeJNWb
0vAnrn3qWfjMMuYs/mS4hes=
=pyP0
-----END PGP SIGNATURE-----

Jul 17 '05 #6

P: n/a
On Tue, 20 Jul 2004 18:28:47 +0200, Anders K. Madsen wrote:
On Tue, 20 Jul 2004 15:24:59 GMT
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote:
On Tue, 20 Jul 2004 07:15:13 -0700, haoren wrote:

[SNIP]

You should _never_ be echoing a < or > char to the browser, this
should_always_ be &lt; or &gt;.


That's interesting... How do you output HTML from PHP then? :-p

What he's trying to say is that if you want a < or a > to show up in
your browser, you should use &lt; and &gt;, since < and > are the chars
used to delimit HTML tags, so the browser will think that the < or > you
just echo'ed out is part of the HTML.
So, again... If you want < and > to print in e.g. a text, use 'echo
"&lt; and &gt;";'

Madsen

lol oops =)

Yup, that pretty much sums it up.. I was referring to if you wanted to
display < or > chars, they should _always_ be entities rather than their
actual chars.

FWIW.. I rarely output HTML from PHP... gotta love Smarty ;)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #7

P: n/a
I should have put the question in the context. In fact, the string is
received from user's input to the form of a browser. Thus, echo
htmlspecialchars($str); can solve the problem.

Thanks Michael and all you guys.
haoren


Michael Fesser <ne*****@gmx.net> wrote in message news:<9t********************************@4ax.com>. ..
.oO(haoren)
How can I echo a string that contain <? and <?php?

For example,

$str="test <? and <?php echo";
echo $str;


echo htmlspecialchars($str);

http://www.php.net/htmlspecialchars

Micha

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.