2013-04-10 66 views
0

我有这等页面test.php我有这样的PHP代码运行在此页test1.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” 的内容来获得:

从这条线

<script type="text/javascript"> fid="x8qfp3cvzbxng8e"; v_width=640; 
v_height=360; </script> 

输出应和:

x8qfp3cvzbxng8e

Wath我必须做什么?

+0

不要以为/ @ fid会工作 – njzk2 2013-04-10 13:01:17

+0

这不行! – 2013-04-10 13:47:46

回答

0

,如果你只想要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; 
+0

我是诺贝我需要完整的代码,我不知道如何使用它。 – 2013-04-10 13:30:50

+0

我把这个代码,但它给了我这个新的“%VAR_PLACE%” – 2013-04-10 13:47:22

+0

我现在编辑测试 – 2013-04-10 13:47:53