Christian Eric Paran
Christian Eric Paran

Reputation: 1010

Output "<"/">" Character in HTML

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

Answers (4)

cHao
cHao

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 &lt; to display a < in HTML, and &gt; for >.

If the data will be dynamic, use htmlentities or htmlspecialchars to do the above encoding for you prior to printing it.

Upvotes: 5

Baba
Baba

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

Alister Bulman
Alister Bulman

Reputation: 35149

You need to convert the htmlspecialchars to the appropriate HTML entities - such as '&lt;'

Upvotes: 2

Quentin
Quentin

Reputation: 943601

Represent characters with special meaning (<&lt; & &&amp; 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

Related Questions