Jump to content

php htmlentities not working


Max Castril
 Share

Recommended Posts

Hi everyone.

 

Anyone noticed that htmlentities() aren't working in php?  Nor are htmlspecialcharacters.

See: https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_string_htmlentities

Any ideas??

I have also written a quick and dirty test routine which I have run on my server:

<?php
if(isset($_POST['char']))
    {
    $char = $_POST['char'];
    $char2 = htmlspecialchars($char);
    $char3 = htmlentities($char);
    echo 'Teclado:  ' . $char . '<br /><br />';
    echo 'Repuesta 1:  ' . $char2 . '<br /><br />';
    echo 'Repuesta 2:  ' . $char3;
    }
echo '<form action = "char_test.php" method = "post">
<input type = "text" name = "char" size = "6">
<input type = "submit" name = "submit" value = "Search!" />
</form>';

It runs but doesn't return any ASCII entities.

 

HELP!!

Max

Link to comment
Share on other sites

It works fine for me. The browser is parsing these entities before showing them to you on the screen, so you won't see something like "&lt;". If you want to see the entity codes without the browser parsing them then output your content with a plain text header.

<?php
if(isset($_POST['char'])) {
    header('Content-Type: text/plain');
    $char = $_POST['char'];
    $char2 = htmlspecialchars($char);
    $char3 = htmlentities($char);
    echo "Teclado:  {$char}\r\n";
    echo "Repuesta 1:  {$char2}\r\n";
    echo "Repuesta 2:  {$char3}\r\n";
    exit;
}
?>
<form method="POST">
  <input type="text" name="char" size="6">
  <input type="submit" name="submit" value="Search!" />
</form>

 

Link to comment
Share on other sites

Thanks Ingolme but it still doesn't work.  I have tried evading the browser by this:

<?php

$char = 'á';
    $char2 = htmlspecialchars($char);
    $char3 = htmlentities($char);
    if($char3 == '&aacute;')
        {
        echo 'DONE!' . $char3;
        }
    else
        {
        echo 'DID NOT!';
        }
?> 

This code shouldn't be affected by the browser at all, but one interesting thing is that the browser doesn't even recognize the character:

DID NOT!Teclado: �

Repuesta 1:

Repuesta 2:      

Link to comment
Share on other sites

SciTE.exe

 

donesuk - I tried this:

<!doctype html><html lang = "es"><head><meta http-equiv="content-type" content="text/plain"></head><body>

but no juice.

 

Now have:

<?php
header('Content-type: text/html; charset=utf-8');
    $char = 'á';
    var_dump($char);
    
    $char2 = htmlspecialchars($char);
    $char3 = htmlentities($char);
    if($char3 == '&aacute;')
        {
        echo 'DONE!' . $char3;
        }
    else
        {
        echo 'DID NOT!';
        }
        
    echo 'Teclado:  ' . $char . '<br /><br />';
    echo 'Repuesta 1:  ' . $char2 . '<br /><br />';
    echo 'Repuesta 2:  ' . $char3;
    echo ' </body>';
    ?>

 

Still no juice

 

Link to comment
Share on other sites

The problem is that your text editor is generating an ANSI or ISO-8859-1 file, but the PHP engine things it's UTF-8 so it is being misinterpreted. You code editor, Notepad, Visual Studio, Sublime, Atom.io, DreamWeaver, whatever it is, needs to save the file with a UTF-8 encoding.

Link to comment
Share on other sites

I am using SciTE 

 

This is the header for SciTEDoc.html (I cannot find how to see the text editor encoding.):

<?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta name="generator" content="HTML Tidy, see www.w3.org" />
    <meta name="generator" content="SciTE" />
    <meta http-equiv="Content-Type" content="text/html" />
    <title>
      SciTE
    </title>
<style type="text/css">

 

 

Link to comment
Share on other sites

I don't know of any most popular editor, each person just goes with what works best for them. I mostly use Notepad++ and Atom at my job. I hear Sublime is popular, but it costs money.

I don't know much about SciTE, but from what I saw of it, it looks pretty primitive. It might be useful for editing local files on a Linux machine, but I'm not sure it has enough features to do serious software development.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...