Reputation: 1010
I'm trying to display "<"/">" as String in HTML not as a tag.
I have a database that contains user's Fullname and Email and when I want display them both I have a format like this:
Christian Eric Paran <[email protected]>
but I it only display like this:
Christian Eric Paran
is there a way to Display this in HTML from PHP?
Upvotes: 3
Views: 2112
Reputation: 86525
Problem is, the <
and >
are used by HTML to delimit tags. The email address thus ends up parsed as an HTML tag, and ends up hidden by most if not all browsers.
Use <
to display a <
in HTML, and >
for >
.
If the data will be dynamic, use htmlentities
or htmlspecialchars
to do the above encoding for you prior to printing it.
Upvotes: 5
Reputation: 95121
Since you are dealing with email address am sure you would need some flexibility dealing with how you display the result it not just hiding the tag
I would recommend http://php.net/mailparse_rfc822_parse_addresses
$email = mailparse_rfc822_parse_addresses("Christian Eric Paran <[email protected]>") ;
echo $email[0]['display'] ; // Christian Eric Paran
echo $email[0]['address'] ; // [email protected]
If you don't have mail parse installed you can use this
$email = parse_addresses ( "Christian Eric Paran <[email protected]>" );
echo $email ['display']; // Christian Eric Paran
echo $email ['address']; // [email protected]
parse_addresses
function
function parse_addresses($address) {
$info = array ();
preg_match_all ( '/\s*"?([^><,"]+)"?\s*((?:<[^><,]+>)?)\s*/', $address, $matches );
$info ['display'] = $matches [1] [0];
$info ['address'] = str_replace ( array (
"<",
">"
), "", $matches [2] [0] );
return $info;
}
Upvotes: 0
Reputation: 35149
You need to convert the htmlspecialchars to the appropriate HTML entities - such as '<
'
Upvotes: 2
Reputation: 943601
Represent characters with special meaning (<
→ <
& &
→ &
being the ones you need to worry about in text nodes) in HTML using character references.
The htmlspecialchars
function will convert for you.
print htmlspecichars("Christian Eric Paran <[email protected]>");
Upvotes: 6