Jump to content

php htmlentities not working


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 post
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 post
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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...