Php 在div中查找链接和信息
我需要在我的数据库链接和外部网站的信息添加到特定的div的内容 HTML就像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 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”;
}
在所有divclass=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,,'');