Re: Escape characters in PHP ?

From: Date: Tue, 16 Nov 1999 11:55:56 +0000
Subject: Re: Escape characters in PHP ?
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-12598@lists.php.net to get a copy of this message
Mukul Gandhi writes: > Hi, > I have a string stored in a variable $var as $var = 'ABCDEFGH:<MNRPQR>'; > But when I do echo $var; it shows output ABCDEFGH: . I want the full string > to be echoed i.e ABCDEFGH:<MNRPQR> . I suppose < & > are special > characters in PHP. What is the way to escape these characters ? > > regds > -mukul > > -------------------------------------------------------------- > Bharti Cellular Limited, New Delhi, India Close, but no cigar. :) They're special characters in HTML. If you run this script on the command line (requires CGI PHP to be built first) or even just 'View source' in your browser, you'll probably find that $var is printing fine. It's just that your browser sees '<', and thinks a tag is starting. However, since browsers ignore tags they don't understand, your browser effectively just tosses the 'MNRPQR' and closing tag '>'; since it didn't understand the tag, it doesn't do anything else about it (such as display it). To get around this, you'll want to use the htmlspecialchars() function, which encodes these characters into HTML entities. This allows them to just be shown without the browser trying to do anything special with them. The documentation for the function is here: http://www.php.net/manual/function.htmlspecialchars.php3 The practical upshot of all this is: <?php $var = 'ABCDEFGH:<MNRPQR>'; echo htmlspecialchars( $var ); ?> Torben

« previous php.dev (#12598) next »