# 解析包含特定短語的鏈接

i have this script. i need is so that it only writes the links that contain "/product-product/" to the file items.txt. Well, not the wohle link but the 10 didget item-nr product-product/1007687980

$keyword=$_SERVER['QUERY_STRING'];
$site=1; while ($site<30) {
$content = file_get_contents('http://www.domain.com/?keywords='.$keyword .'&x=0&y=0&pagecount='.$site.'&sort=sort');$html = $content;$dom = new DomDocument();
@$dom->loadHTML($html);
$urls =$dom->getElementsByTagName('a');

$lookfor='http://www.domain.com'; foreach ($urls as $url){ if(substr($url->getAttribute('href'),0,strlen($lookfor))==$lookfor){
$tubeurl = str_replace ("http://www.domain.com","",$url->getAttribute('href'));
$tubeurl = substr($tubeurl, strpos($tubeurl,"/product-product/100")+17, 10); file_put_contents("items.txt", "" .$tubeurl. "
", FILE_APPEND | LOCK_EX);// this line must remain, it makes it so that there is a new line  \n wouldn't work

}
} $site++; echo$site;}


## 最佳答案

$keyword=$_SERVER['QUERY_STRING'];
$site=1; while ($site<30) {
$content = file_get_contents('http://www.domain.com/?keywords=' .$keyword . '&x=0&y=0&pagecount='.$site.'&sort=sort');$html = $content;$dom = new DomDocument();
@$dom->loadHTML($html);
$urls =$dom->getElementsByTagName('a');

$lookfor='http://www.domain.com'; foreach ($urls as $url){ if(substr($url->getAttribute('href'),0,strlen($lookfor))==$lookfor){
$tubeurl = str_replace ("http://www.domain.com","",$url->getAttribute('href'));

preg_match("/^http.*/product-product\/(\d{10})$/",$tubeurl, $matches); file_put_contents("items.txt",$1,
FILE_APPEND | LOCK_EX);//this line must remain, it makes it so that there is a new line  \n wouldn't work

}
}

$site++; echo$site;
}