470,862 Members | 1,825 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

echo a string that contain <? and <?php

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
7 4981
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
.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
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
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
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
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
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.

Similar topics

6 posts views Thread by The Plankmeister | last post: by
5 posts views Thread by Jonny T | last post: by
3 posts views Thread by J Trost | last post: by
4 posts views Thread by Michael G | last post: by
10 posts views Thread by vunet.us | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.