Reputation: 39
I'm trying to echo this text :
echo $string = "Hello <username> !";
But what I get is Hello !
, it removed my <username>
.
Do you, please, know how to fix that ?
Upvotes: 0
Views: 308
Reputation: 1951
When the <
and >
characters are rendered the browser interprets them as a new html tag. To prevent this from happening you should use the php htmlspecialchars()
function to translate them into their html entity values ie <
and >
.
$username = htmlspecialchars("<username>");
echo $string = "Hello $username!";
http://php.net/manual/en/function.htmlspecialchars.php
Upvotes: 0
Reputation: 1291
Maybe, the php works fine.
But, when the HTML is interpreted in your browser, it goes to a username
tag
Replace <
and >
with html entities should work.
$string = "Hello <username> !";
echo htmlentities($string);
Upvotes: 1
Reputation: 2882
Try this:
echo $string = "Hello <username> !";
This happened because your browser treated <username>
as a HTML tag,
You can use htmlspecialchars
Upvotes: 1
Reputation: 15141
Try this hope this will help you out. Here <
and >
are the part of html entities.
<?php
ini_set('display_errors', 1);
$string = "Hello <username> !";
echo $string = htmlentities($string);
Output: Hello <username> !
Upvotes: 1