473,704 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5171
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 htmlspecialchar s($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($s tr);
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*@WINDOZEdig iserv.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/JASHcRAuwPAJ4o6 pf0fZ5cP6lTfzCT x1oPTlo0lACeJNW b
0vAnrn3qWfjMMuY s/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*@WINDOZEdig iserv.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
htmlspecialchar s($str); can solve the problem.

Thanks Michael and all you guys.
haoren


Michael Fesser <ne*****@gmx.ne t> 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 htmlspecialchar s($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
2361
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 is the most proper way of doing such? Would it be to do the full monty?:
9
4780
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 variable $noFile, it is print "1" instead of "true". Why does it do this? The loop is failing to run a second time, for reasons I can not fathom. debugNotes tells me that the count of $dirArray is 10, and the file hasn't been found yet, so I...
5
2379
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
5706
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 to convert XML like this: <?xml version="1.0" encoding="UTF-8"?> <java version="1.4.2_03" class="java.beans.XMLDecoder"> <object class="javax.swing.JButton"> <string>Hello, world</string>
0
2064
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 <esql:get-xml> the tags are not escaped, but only the first part of the database field is displayed. The content of the database field is: "<h1>Title</h1><h2>Subtitle</h2>"
4
9078
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
5520
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 response.write (because it does not split) is: Microsoft VBScript runtime error '800a0009' Subscript out of range: '' /get_item.asp, line 55
2
7012
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. for ($i=1;$i<10;$i++) { Tie::CheckVariables->on_error(sub{print "ERROR!"}); tie my $data,'Tie::CheckVariables','integer';
3
8191
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 know about ".innerText", but that's not portable; some browsers convert escapes when reading from innerText and some don't. John Nagle
0
8767
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8689
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5933
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3137
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.