Max Castril Posted November 27, 2019 Share Posted November 27, 2019 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 More sharing options...
dsonesuk Posted November 27, 2019 Share Posted November 27, 2019 In chrome press F12, in tab 'Elements select the element right click -> Edit as HTML What does it show now. Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 Is that what you want? Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 Can you get https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_string_htmlentities to work on your computer? Link to comment Share on other sites More sharing options...
Ingolme Posted November 27, 2019 Share Posted November 27, 2019 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 "<". 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 More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 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 == 'á') { 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 More sharing options...
Ingolme Posted November 27, 2019 Share Posted November 27, 2019 It is likely that your PHP file is not encoded as UTF-8. Your code editor should have the ability to set the encoding to UTF-8. Link to comment Share on other sites More sharing options...
dsonesuk Posted November 27, 2019 Share Posted November 27, 2019 Use var_dump() before if condition for both. Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 Result was: string(1) "�" Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 I'm rapidly getting out of my depth here. Link to comment Share on other sites More sharing options...
Ingolme Posted November 27, 2019 Share Posted November 27, 2019 It's not a PHP setting, you need your text editor to set the encoding of your PHP file to UTF-8. Which code editor are you using to write code? Link to comment Share on other sites More sharing options...
dsonesuk Posted November 27, 2019 Share Posted November 27, 2019 Try adding header('Content-type: text/html; charset=utf-8'); To top of page. Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 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 == 'á') { 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 More sharing options...
Ingolme Posted November 27, 2019 Share Posted November 27, 2019 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 More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 string(1) "�" DID NOT!Teclado: �Repuesta 1:Repuesta 2: Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 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 More sharing options...
Ingolme Posted November 27, 2019 Share Posted November 27, 2019 This editor is not familiar to me, but here is an article indicating how to make SciTE save files with UTF-8 encoding: https://cstan.io/?p=8557&lang=en Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 Well, I copied and pasted the text to Notepad and uploaded it and got: string(2) "á" DONE!áTeclado: áRepuesta 1: áRepuesta 2: á Thanks to all for your help Link to comment Share on other sites More sharing options...
Max Castril Posted November 27, 2019 Author Share Posted November 27, 2019 @Ingolme - I thought all programmers used SciTE. What is the most popular one? Link to comment Share on other sites More sharing options...
Ingolme Posted November 27, 2019 Share Posted November 27, 2019 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now