English 中文(简体)
How to get Content-type using html simple dom?
原标题:

I tried find( meta[http-equiv="Content-type"] ) but it failed to retrieve that information.

最佳回答

SimpleHTMLDom doesn t use quoted string literals in the selector. It s just elem[attr=value]. And the comparison of value seems to be case-sensitive (there may be a way to make it case-insensitive, but that I don t know)*

E.g.

require  simple_html_dom.php ;
$html = file_get_html( http://www.google.com/ );
// most likely one one element but foreach doesn t hurt
foreach( $html->find( meta[http-equiv=content-type] ) as $ct ) { 
  echo $ct->content, "
";
}

prints text/html; charset=ISO-8859-1.

*edit: yes, there is a way to perform a case-insensitive match, use *= instead of =

find( meta[http-equiv*=content-type] )

edit2: btw that http-equiv*=content-type thingy would also match <meta http-equiv="haha-no-content-types"... (it only tests if the string is somewhere in the attribute s value). But it s the only case-insensitive function/operator I could find. I guess you can live with it in this case ;-)
edit 3: It uses preg_match( .../i ) and the pattern/selector is directly passed to that function. Therefore you could do something like http-equiv*=^content-type$ to match http-equiv="Content-type" but not http-equiv="xyzContent-typeabc". But I don t know if this is a warranted feature.

问题回答

The Content-Type is typically part of the http-response headers - not in the body. Where did you get the xml document from?

I would go foreach on $this->find( meta ); in case of differently written content-type - I think that browsers aren t in this case case sensitive, while php might be.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签