我有这个页面test1.php在另一个页面test.php我有这个php代码运行:
<?php
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("http://inviatapenet.gethost.ro/sop/test1.php");
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*[@type='text/javascript']/@fid");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "'n";
}
}
}
?>
但是什么也没显示。
我想从那个页面得到,只有fid="x8qfp3cvzbxng8e":
From this Line
<script type="text/javascript"> fid="x8qfp3cvzbxng8e"; v_width=640;
v_height=360; </script>
输出应该是:
x8qfp3cvzbxng8e
我该怎么做?
如果您只想要fid
内容,请使用此正则表达式
preg_match_all('~fid="(.*?)"~si',$Text,$Match);
print_r($Match);
示例的输出
Array
(
[0] => Array
(
[0] => fid="x8qfp3cvzbxng8e"
)
[1] => Array
(
[0] => x8qfp3cvzbxng8e
)
)
尝试此提取文本,此不显示任何script
内容,但如果您想要可以删除此
function extractText($node) {
if($node==NULL)return false;
if (XML_TEXT_NODE === $node->nodeType || XML_CDATA_SECTION_NODE === $node->nodeType) {
return $node->nodeValue;
} else if (XML_ELEMENT_NODE === $node->nodeType || XML_DOCUMENT_NODE === $node->nodeType || XML_DOCUMENT_FRAG_NODE === $node->nodeType) {
if ('script' === $node->nodeName) return '';
$text = '';
foreach($node->childNodes as $childNode) {
$text .= extractText($childNode);
}
return $text;
}
}
示例 $Text=file_get_contents("http://inviatapenet.gethost.ro/sop/test1.php");
preg_match_all('~fid="(.*?)"~si',$Text,$Match);
$fid=$Match[1][1];
echo $fid;