PHP:从file_get_contents字符串

时间:2016-10-27 04:59:12

标签: php html

我需要对网址执行一系列测试。第一个测试是字数,我有完美的工作,代码如下:

if (isset($_GET[article_url])){
    $title = 'This is an example title';
    $str = @file_get_contents($_GET[article_url]);
    $test1 = str_word_count(strip_tags(strtolower($str)));
    if($test1 === FALSE) { $test = '0'; }
    if ($test1 > '550') {
        echo '<div><i class="fa fa-check-square-o" style="color:green"></i> This article has '.$test1.' words.';
    } else {
        echo '<div><i class="fa fa-times-circle-o" style="color:red"></i> This article has '.$test1.' words. You are required to have a minimum of 500 words.';
    }       
}

接下来我需要从$ str获取所有h1和h2标签并测试它们以查看是否包含文本$ title并且如果是,则回显yes,否则返回no。我不确定该怎么做。

我正在寻找一种纯PHP的方法,无需安装php库或第三方功能。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码。

if (isset($_GET[article_url])){
    $title = 'This is an example title';
    $str = @file_get_contents($_GET[article_url]);

    $document = new DOMDocument();
    $document->loadHTML($str);

    $tags = array ('h1', 'h2');
    $texts = array ();
    foreach($tags as $tag)
    {
      //Fetch all the tags with text from the dom matched with passed tags
      $elementList = $document->getElementsByTagName($tag);
      foreach($elementList as $element)
      {
         //Store text in array from dom for tags
         $texts[] = strtolower($element->textContent);
      }
    }
    //Check passed title is inside texts array or not using php
    if(in_array(strtolower($title),$texts)){
        echo "yes";
    }else{
        echo "no";
    }
}