ulthane — 2012-02-23T10:11:50-05:00 — #1
Hey everyone, i saw that html5 has a new way of declaring charset, instead of:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
i can put:
My question is, do old browsers support that shorthand? should i expect any issues with it?
And regarding a php question, im trying to get some text from external links, the text is returned as '???' even if i use
header('Content-Type: text/html; charset=utf-8);
I tried anpther encoding (my language - hebrew) and it worked:
header('Content-Type: text/html; charset=windows-1255');
But the question is if the two wont conflict with each other... (the utf-8 declared in the html and windows-1255 declared in php)
Thanks for the help,
michael_morris1 — 2012-02-23T12:50:34-05:00 — #2
It is far more effective and far more efficient to declare http headers in the actual header of the document rather than use the http-equiv tags. When applied the content type in particular those tags are a joke: by the time the browser reaches the tag it has already chosen a charset and language. At best you waste the client's time restarting the page render. At worst the client happily ignores your tag (and most browsers do).
You already know how to set the headers in PHP. The http-equiv tags are redundant and unnecessary.
ulthane — 2012-02-23T15:59:42-05:00 — #3
So i didn't understand what you say is i should delete the meta tag completely and only put php header declaration? it then wont be visible to the client (dunno if it has any downsides or...)
michael_morris1 — 2012-02-24T09:11:02-05:00 — #4
... HTTP 101 ...
A document transmitted via the HTTP protocol will have two sections - a header and a body. The way modern browsers work, you never see the header, but they are there. These are the response headers for Google.
Date: Fri, 24 Feb 2012 13:59:47 GMT
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
X-XSS-Protection: 1; mode=block
After this information comes the MIME encoded body of the document. If it's text it will be relatively readable.
PHP can control, through the header function, the contents of any of these lines. This allows you to modify the response code, caching and so on. Whatever you don't populate your webserver program populates for you according to its own settings.
meta http-equiv tags will, in theory, override these properties. But it's more efficient to pass the correct desired value in the header in the first place. Also the content-type header cannot be changed after rendering of the document has started, so http-equiv="Content-Type" is useless and will be ignored. The same applies to the Content-Encoding and Content-Length properties. Meta http-equiv tags are primarily used for setting specific caching rules in otherwise static html documents, and they are quite effective in that role.
ulthane — 2012-02-24T12:36:58-05:00 — #5
hey , thanks for the information, i understood now, i got another question thought, is there any way to change encoding for only a part of the php script? (like in a function only)
Im using UTF-8 for my website, but i must use windows-1255 to get page titles from external links, cuz UTF-8 always return '???'
Any clue? or a workaround?
michael_morris1 — 2012-02-24T12:41:46-05:00 — #6
Character coding must be uniform for the file.
ulthane — 2012-02-24T16:54:16-05:00 — #7
isnt there any way to get external page titles without being dependent on encoding ...?
michael_morris1 — 2012-02-24T21:23:52-05:00 — #8
$SERVER['REQUESTURI'] holds the file name the outside world is asking for.
jeff_mott — 2012-02-24T23:57:11-05:00 — #9
ulthane, if I understood your latest request correctly, after your script downloads content from some external source, you'll then need to [detect and [url=http://www.php.net/manual/en/function.mb-convert-encoding.php]convert](http://www.php.net/manual/en/function.mb-detect-encoding.php) its encoding.
ulthane — 2012-02-25T04:18:29-05:00 — #10
hey Jeff thanks for the answer however i noticed that from 2 different pages with same encoding i get different results (one as '???' and the other as normal...) so i guess it was not an encoding issue, or at least it will be hard to detect and fix
So I just checked the returned title with a preg_match and if it doesnt contain the right characters im looking for it will be named "link" if anyone is interested in the solution here it is, it works fine but its a little bit slow ....
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11');
$doc = new DOMDocument();
$xpath = new DOMXPath($doc);
$title = trim($xpath->query('//title')->item(0)->nodeValue);
if (preg_match('/[^a-z0-9 ]/i', $title) || $title=='')