Solved

htmlentities not working properly on live server, only localhost

Posted on 2016-11-09
7
61 Views
Last Modified: 2016-11-10
For some reason things that work on my localhost are not working on a live server. For instance:

$prod_name = htmlentities($row['prod_name'], ENT_QUOTES, "UTF-8");

Open in new window


works on my localhost but on the live server the title is showing funny characters like this � )

Does anybody have a clue why this might be?
0
Comment
Question by:Black Sulfur
  • 3
  • 3
7 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41881019
Your character encoding has to be 100% consistent across all parts of the platform.  Check your HTML and PHP declarations and make sure they match.
https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html
0
 

Author Comment

by:Black Sulfur
ID: 41881089
A very interesting read, thanks!

Funny enough, I found the problem was actually caused by minifying my page. I used the GTmetrix minifier and that caused the problem. So, since I can't use that one, I don't suppose you know of a good one that minifies html and PHP. I suspect that is what broke it, the fact that I have PHP in there as well.

I tried this one:

https://htmlcompressor.com/compressor/

but I still get notified that I need to minify my HTML so obviously it isn't good enough.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41881165
I'm never sure of the value you get from minifying PHP or HTML.  I can see minifying JavaScript, especially for big libraries like jQuery, and maybe CSS.  But the debugging problems you get from minification of PHP and HTML are just not worth the savings.  And in related matters, if the minifier changes something as basic as your character set encoding, you are doing the right thing to discard it completely.

Where / how do you get the notification that you need to minify your HTML?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Black Sulfur
ID: 41881184
yeah, it isn't something I particularly want to do but I am trying to get a good score. I have managed to minify it without breaking it but Google and GTmetrix still say it isn't minified enough.

https://developers.google.com/speed/pagespeed/insights/

It says:

Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up download and parse times.
Minify HTML for the following resources to reduce their size by 811B (14% reduction).

Minifying http://mysite.com could save 811B (14% reduction) after compression.
0
 

Author Comment

by:Black Sulfur
ID: 41881187
P.S. I even minified my minify and I still get the message.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41881293
811B
Really?  You could transfer that a million times and not use as much time as we've spent on this question!  This is one of those things like milking a mouse -- no matter how much effort you put into the process, you just don't get much out.  I would recommend that you move on to something more important :-)  Check Peter Norvig's article (look for "Answers") and you can see where the center-of-gravity rests in some computer applications.  TL;DR there are a lot of things not worth optimizing!
http://norvig.com/21-days.html

FWIW, here is my hastily created example showing the places you probably want to use UTF-8.  
<?php // demo/utf8_example.php
/**
 * Making things work with UTF-8
 *
 * http://php.net/manual/en/function.mb-internal-encoding.php
 * http://php.net/manual/en/function.mb-regex-encoding.php
 * http://php.net/manual/en/function.mb-http-output.php#114843

 * http://php.net/manual/en/function.mysql-set-charset.php
 * http://php.net/manual/en/mysqli.set-charset.php
 * http://php.net/manual/en/ref.pdo-mysql.connection.php
 *
 * http://www.w3schools.com/TAgs/att_meta_charset.asp
 *
 * https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html
 * https://www.experts-exchange.com/articles/12335/PHP-and-MySQLi-Table-Maintenance.html
 */
error_reporting(E_ALL);


// MAKE SURE THAT PHP WORKS WITH UTF-8
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
mb_http_output('UTF-8');


// MAKE SURE THAT MySQL WORKS WITH UTF-8 (DEPRECATED)
mysql_set_charset('utf8mb4');

// MAKE SURE THAT MySQLi WORKS WITH UTF-8
$mysqli = new mysqli('localhost', 'username', 'password', 'test');
$mysqli->set_charset("utf8mb4");

// MAKE SURE THAT PDO WORKS WITH UTF-8 (RELEASE-DEPENDENT)
$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'username', 'password');


// END OF PHP, LOOK AT HTML
?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>

<!-- MAKE SURE THAT HTML WORKS WITH UTF-8 -->
<meta charset="utf-8" />

</head>
<!-- etc etc ... -->

Open in new window

1
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41882980
We've gotten a lot of questions about Google Pagespeed.  Some of their suggestions are worth following, some are not, and some are doable only when you own your own servers and wrote your own server code.  I rarely if ever minify any code.  

On sites where a lot of images are downloaded, I may enable 'gzip' compression.  That is a negotiation between the browser and the server.  Other than enabling it, it is not something that you do.  The server estimates whether it will take longer to compress it than to just send it.  A file with only 5800 bytes is not likely to be compressed.

Another thing I almost never do is set expiration dates for image, CSS, and JavaScript files.  My customers change those files way too often for that to be a good idea.
1

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question