Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在div中查找链接和信息_Php_Regex - Fatal编程技术网

Php 在div中查找链接和信息

Php 在div中查找链接和信息,php,regex,Php,Regex,我需要在我的数据库链接和外部网站的信息添加到特定的div的内容 HTML就像 <div class="last"> <a href="...">test</a> <b>title</b> </div> <div class="last"> <a href="...">test</a> <b>title</b> </div> 标题 标题

我需要在我的数据库链接和外部网站的信息添加到特定的div的内容

HTML就像

<div class="last">
  <a href="...">test</a>
  <b>title</b>
</div>
<div class="last">
  <a href="...">test</a>
  <b>title</b>
</div>

标题
标题
我用这个

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
     $url2 = $href->getAttribute('u');

    echo $url . '<br />';
    echo $url2 . '<br />';
    echo $target_url . '<br />';

    //storeLink($url,$target_url);
    //echo "<br />Link stored: $url";
}
$dom=newdomdocument();
@$dom->loadHTML($html);
//抓取页面上的所有内容
$xpath=newdomxpath($dom);
$hrefs=$xpath->evaluate(“/html/body//a”);
对于($i=0;$i<$hrefs->length;$i++){
$href=$hrefs->item($i);
$url=$href->getAttribute('href');
$url2=$href->getAttribute('u');
回显$url。“
”; 回显$url2。“
”; echo$target_url.“
”; //storeLink($url,$target\u url); //echo“
存储的链接:$url”; }
在所有div
class=last
中只包含
a href
b
的正则表达式是什么?

使用此函数

function get_string($string, $start, $end)
{
   $found = array();
   $pos = 0;
   while( true )
   {
      $pos = strpos($string, $start, $pos);
      if ($pos === false) 
      { // Zero is not exactly equal to false...
         return $found;
      }
       $pos += strlen($start);
       $len = strpos($string, $end, $pos) - $pos;
       $found[] = substr($string, $pos, $len);
    }
}

$html='<div class="last"><a href="...">test</a><b>title</b></div><div class="last"><a href="...">test</a><b>title</b></div>';
函数get_string($string,$start,$end) { $found=array(); $pos=0; while(true) { $pos=strpos($string,$start,$pos); 如果($pos==false) {//0不完全等于false。。。 返回$found; } $pos+=strlen($start); $len=strpos($string,$end,$pos)-$pos; $found[]=substr($string,$pos,$len); } } $html='titlettle'; 从Html获取锚点

get_string($html,'class="last"','</a>');
get_字符串($html,'class=“last”',“”);
类似于提取数据

get_string($html,'<b>','</b>');
get_字符串($html,,'');