簡體   English   中英

從網站獲取html字符集-NON UTF-8格式的元標記

[英]Get the html charset from a site - Meta tags in a NON UTF-8 format

我嘗試檢索封裝在

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">

html網站。

在上面給定的html上,我想提取“ iso-8859-7”部分,您知道我該怎么做嗎?

注意:它可以是任何值。

我需要它,因為有時我將需要網站的編碼才能檢索元標記並對其進行正確編碼。

注意:我已經通過php Curl或file_get_contents檢索了html的內容。

您收到一個字符串嗎? 如果是這樣,您可以只使用RegEx來檢索它。

$string = '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">'; // your string

$matches = array(); 
preg_match('/charset=[^"]*/', $string, $matches); // retrieve charset and the value
preg_replace('/charset=/', '', $matches[0]); // remove the 'charset='

您將以字符串形式獲取值。 如果您有html文件開頭的上一個答案應該會有所幫助。

編輯:如果您想了解更多有關ReGex的信息,可以閱讀以下內容:

http://www.tutorialspoint.com/php/php_regular_expression.htm

對於我所做的; 我只是要求“ charset =”,其后的所有內容都不是引號。 [^“] *。

您可以使用JQuery

如果只有元數據,則可以使用這種方式

var myValue = $('head meta').get(0).attr("content");

或者如果你有幾個

$("head meta").each(function () {
  alert( $(this).attr("content");
});

在PHP中,您可以使用

$ tags = get_meta_tags(' http://www.example.com/ ');

  echo $tags['author']; // name echo $tags['keywords']; // php documentation echo $tags['description']; // a php manual echo $tags['geo_position']; // 49.33;-86.59 

這是從http://php.net/manual/en/function.get-meta-tags.php PHP DOC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM