473,385 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5145
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: The Plankmeister | last post by:
Hi... I seem to remember reading somewhere about the proper way of doing this sort of thing in the middle of some html, for example: <a href="<?= $some_url_or_other ?>">Click this!</a> What...
9
by: lawrence | last post by:
In the following loop, at the end, the method debugNotes() is printing out some notes for me to read, so I can figure out what is going on in my script. Where I try to print out the value of the...
5
by: Jonny T | last post by:
hi, i want to echo the string '<?php' in a php script like : echo "<?php"; but when i try nothing gets displayed ... if I use echo "<\?php";
3
by: J Trost | last post by:
I was wondering if anyone knows if it is possible to do basic string replacement using XSLT even though the strings being replaced may contain "<" and ">". Here is my problem: I need to be able...
0
by: Arne Schirmacher | last post by:
I want to display a MySQL database field that can contain HTML markup. If I use <esql:get-string> then I get all of the database field, but all tags are escaped which is not what I want. If I use...
4
by: Michael G | last post by:
Some of the online docs say that you can use echo <<<END all sorts of text and statements... END; but if the following produces a parse error at line 14 which is the closing
10
by: vunet.us | last post by:
Hello, I use XMLHTTP to get an HTML of another page. Then, I need to cut some middle part of that HTML string but I have problems doing it (see note in caps below). The error I have generated at...
2
by: pnsreee | last post by:
Hi All, I have the following code. The array @sub_data will contain integers or a string"NO". I have to validate if the array contain integer. If it contain "NO" then no need to validate. ...
3
by: John Nagle | last post by:
I have XML replies in a DOM which contain entity escapes, like "&amp;". What's the proper way to replace them with the ordinary characters? Preferably something that will work in most browsers? I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.