【问题标题】:Text extractor php文本提取器 php
【发布时间】:2013-04-10 12:59:07
【问题描述】:

我有这个页面 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" 的内容:

从这条线

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

输出应该是:

x8qfp3cvzbxng8e

我必须做什么?

【问题讨论】:

  • 不要认为 /@fid 会起作用

标签: php


【解决方案1】:

如果您只想要 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;

【讨论】:

  • 我输入了这个代码,但它给了我这个新的“%VAR_PLACE%”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
相关资源
最近更新 更多