1

ボディタグ内のコンテンツを取得したい..それらを単語として分離し、単語を配列に取得したい..phpを使用していますこれは私がやったことです

$content=file_get_contents($_REQUEST['url']);
$content=html_entity_decode($content);
$content = preg_replace("/&#?Ã[a-z0-9]+;/i"," ",$content); 
$dom = new DOMDocument;
@$dom->loadHTML($content);
$tags=$dom->getElementsByTagName('body');
foreach($tags as $h)
{
echo "<li>".$h->tagName;
 getChilds2($h);    

function getChilds2($node)
{

  if($node->hasChildNodes())
   { 
   foreach($node->childNodes as $c)
    { 
        if($c->nodeType==3)
         {

           $nodeValue=$c->nodeValue;   
            $words=feature_node($c,$nodeValue,true);
           if($words!=false)
             {
              $_ENV["words"][]=$words;

             } 

             else if($c->tagName!="")
             {


             getChilds2($c);  
              }
        }
      }

   }
  else
  {
   return;
  }
}
function feature_node($node,$content,$display)
{

 if(strlen($content)<=0)
  {
   return;
   }

 $content=strtolower($content);
 $content=mb_convert_encoding($content, 'UTF-8',
      mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
    $content= drop_script_tags($content);       
$temp=$content;
$content=strip_punctuation($content);
$content=strip_symbols($content);
$content=strip_numbers($content);
$words_after_noise_removal=mb_split( ' +',$content);
$words_after_stop_words_removal=remove_stop_words($words_after_noise_removal);
if(count($words_after_stop_words_removal)==0)
 return(false);
$i=0;
foreach($words_after_stop_words_removal as $w)
   {

      $words['word'][$i]=$w;
      $i++;
   }

for($i=0;$i<sizeof($words['word']);$i++)
 { 
   $words['stemmed'][$i]= PorterStemmer::Stem($words['word'][$i],true)."<br/>";
 }

 return($words);
}

ここでは、strip_punctuation、strip_symbols、strip_numbers、remove stop_words、porterstemmer などの関数を使用して、ページの前処理を行いました..それらは正常に動作しています..しかし、内容を配列に取得せず、print_r() または echo は何も与えません..help plz ?

4

1 に答える 1

2

ノードを反復処理する必要はありません。

$tags = $dom->getElementsByTagName('body');

DOMNodeList に結果が 1 つだけ表示されます。したがって、テキストを取得するために必要なことは、

$plainText = $tags->item(0)->nodeValue;

また

$plainText = $tags->item(0)->textContent;

個別の単語を配列に入れるには、次を使用できます

$plainTextその結果について

于 2013-02-27T18:33:51.107 に答える