簡體   English   中英

PHP Simple HTML DOM Parser查找字符串

[英]PHP Simple HTML DOM Parser find string

我使用的是PHP簡單的DOM解析器,但它似乎沒有搜索文本的功能。 我需要搜索一個字符串並找到它的父ID。 基本上與正常使用相反。

誰知道怎么樣?

$html = file_get_html('http://www.google.com/');

$eles = $html->find('*');
foreach($eles as $e) {
    if(strpos($e->innertext, 'theString') !== false) {
        echo $e->id;
    }
}

http://simplehtmldom.sourceforge.net/manual.htm

試想一下,任何標簽都有“明文”屬性,並使用標准屬性選擇器。

那么,HTML:

<div id="div1">
  <span>London is the capital</span> of Great Britain
</div>
<div id="div2">
  <span>Washington is the capital</span> of the USA
</div>

可以想象為:

<div id="div1" plaintext="London is the capital  of Great Britain">
  <span plaintext="London is the capital ">London is the capital</span> of Great Britain
</div>
<div id="div2" plaintext="Washington is the capital  of the USA">
  <span plaintext="Washington is the capital ">Washington is the capital</span> of the USA
</div>

PHP解決您的任務只是:

<?php
  $t = '
    <div id="div1">
      <span>London is the capital</span> of Great Britain
    </div>
    <div id="div2">
      <span>Washington is the capital</span> of the USA
    </div>';
  $html = str_get_html($t);
  $foo = $html->find('span[plaintext^=London]');
  echo "ID: " . $foo[0]->parent()->id; // div1
?>

(請記住, <span>標記的“明文”用空格符號右邊填充;這是簡單HTML DOM的默認行為,由常量DEFAULT_SPAN_TEXT定義)

$d = new DOMDocument();
$d->loadXML($xml);
$x = new DOMXPath($d);
$result = $x->evaluate("//text()[contains(.,'617.99')]/ancestor::*/@id");
$unique = null;
for($i = $result->length -1;$i >= 0 && $item = $result->item($i);$i--){
    if($x->query("//*[@id='".addslashes($item->value)."']")->length == 1){
        echo 'Unique ID is '.$item->value."\n";
            $unique = $item->value;
        break;
    }
}
if(is_null($unique)) echo 'no unique ID found';

得到了答案。 整個例子有點長但是有效。 我也展示了輸出。

我們要看的HTML:

<html>
<head>
<title>Simple HTML DOM - Find Text</title>
</head>
<body>
<h3>Simple HTML DOM - Find Text</h3>
<div id="first">
 <p>This is a paragraph inside of div 'first'.
   This paragraph does not have the text we are looking for.</p>
 <p>As a matter of fact this div does not have the text we are looking for</p>
</div>
<div id="second">
 <ul>
  <li>This is an unordered list.
  <li id="love1">We are looking for the following word love.
  <li>Does not contain the word.
 </ul>
 <p id="love2">This paragraph which is in div second contains the word love.</p>
</div>
<div id="third">
 <a id="love3" href="goes.nowhere.com">link to love site</a>
</div>
</body>
</html>

PHP:

<?php
include_once('simple_html_dom.php');

function scraping_for_text($iUrl,$iText)
{
echo "iUrl=".$iUrl."<br />";
echo "iText=".$iText."<br />";

    // create HTML DOM
    $html = file_get_html($iUrl);

    // get text elements
    $aObj = $html->find('text');
    if (count($aObj) > 0)
    {
       echo "<h4>Found ".$iText."</h4>";
    }
    else
    {
       echo "<h4>No ".$iText." found"."</h4>";
    }
    foreach ($aObj as $key=>$oLove)
    {
      $plaintext = $oLove->plaintext;
      if (strpos($plaintext,$iText) !== FALSE)
      {
         echo $key.": text=".$plaintext."<br />"
              ."--- parent tag=".$oLove->parent()->tag."<br />"
              ."--- parent id=".$oLove->parent()->id."<br />";
      }
    }

    // clean up memory
    $html->clear();
    unset($html);

    return;
}

// -------------------------------------------------------------
// test it!

// user_agent header...
ini_set('user_agent', 'My-Application/2.5');

scraping_for_text("test_text.htm","love");
?>

輸出:

iUrl=test_text.htm
iText=love
Found love
18: text=We are looking for the following word love.
--- parent tag=li
--- parent id=love1
21: text=This paragraph which is in div second contains the word love.
--- parent tag=p
--- parent id=love2
25: text=link to love site
--- parent tag=a
--- parent id=love3

這就是他們所寫的!!!!

暫無
暫無

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

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