I'm stuck with this one...
I insert some data into a database table which comes from a form. Sometimes, this data contains html, so I do
$note = htmlentities(mysqli_real_escape_string($this->db, $_POST['note']));
I then store the $note in my database. When retrieving my notes, I check whether there is html code in it, and if there is, I want to have it highlighted using highlight_string(). That part works like a charm. Now here's the problem:
I am displaying the highlighted string in a div container, but it's overflowing because the white spaces are being converted to . I'm trying to remove those non-breaking spaces and replace them with a simple space so the highlighted string will be contained in the div.
I have tried this so far:
$note = html_entity_decode($note);
$note = highlight_string($note);
$note = preg_replace('/ /', '', $note);
I have also tried $note = str_replace(' ', ' ', $note);, to no avail.
Any help is very much appreciated! Thanks :)
highlight_stringfor it to return the valuehtmlentities(mysqli_real_escape_string(NO)). Do not use HTML escaping to "protect" database access; use escaping only when applied to the context for which it was applied - in this case that would mean using htmlentities when output as appropriate, and not before. At the very least, use mysqli_real_escape_string (MRES) last prior to building the SQL string. If MRES doesn't make the data safe, then applying HTML escaping won't do any good. But better, don't even use MRES - use parameterized queries.$insert_this_exactly = MRES(htmlentities($raw_data)). That is, only the result of MRES is guaranteed safe when used as a string literal content in SQL command text. However, I would doMRES($raw_data). Then you have the real data in the database, not some encoded junk that won't add any "security" against SQL injection - and has to be decoded later on! When displaying useecho "<span>" + htmlentities($original_raw_data) + "</span>";or whatnot. That is, only encode for a specific context when used in that context.