views:

49

answers:

2

i'm calling the htmlspecialchars function inside another function in a class, but when i do this, even though the data displays, it removes all formatting showing the data in a single line.

this is the code:

class Name {
    . .. .
    public function h($s) 
    {
    echo htmlspecialchars($s, ENT_QUOTES);
     }

    public function formatQuotes($row)
    {

    return "<p id=\"ab_quotes\">" . this->h($row['cQuotes']) . "</p>"
    . "<p id=\"ab_author\">" . this->h($row['vAuthor']) . "</p>";             
    }

}

if i remove the reference to htmlspecialchars function, it displays the data as it should.

UPDATE:

this is the css which i've applied:

p#ab_quotes{
    font-size: 22px;
    word-wrap: break-word;
    position: absolute;
    top: 80px;
    left: 5px;
    padding: 8px 6px;
    }

p#ab_author {
    font-size: 15px;
        position: absolute;
    top: 200px;
    right: 5px;
    padding: 8px 6px;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #EB3B55;
    }

there is no html within $row['cQuotes'] and Author variables. it is the css formatting which is removed when the htmlspecialchars is implemented.

another thing, that i noticed was that if i removed ENT_QUOTES, it works, but again with it, it removes the formatting. why is this so?

+1  A: 

What do you mean by formatting? If you mean HTML code, htmlspecialchars will replace these characters crucial to HTML with their entities:

  • '&' (ampersand) becomes '&'
  • '"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
  • ''' (single quote) becomes ''' only when ENT_QUOTES is set.
  • '<' (less than) becomes '<'
  • '>' (greater than) becomes '>'

(taken from here: http://php.net/manual/en/function.htmlspecialchars.php)

Of course none of the formatting will have effect then. That's the point of htmlspecialchars().

Richard Knop
without using the object oriented approach, the function htmlspecialchars works and it displays all the *css* formatting. but using classes, it does not work.
fuz3d
+1  A: 

The problem might be that the function h() needs to return the data rather than echo it (based on how you are using the result of h())

Yacoby
thank you for pointing that out! :)
fuz3d