正则表达式不太正确

时间:2013-02-15 00:22:51

标签: php regex

我有一个站点爬虫,它显示了一个网址列表,但问题是我不能为我的生活得到最后一个正则表达式。 所有网址最终都列为:

http://www.website.org/page1.html&--EFTTIUGJ4ITCyh0Frzb_LFXe_eHw
http://website.net/page2/&--EyqBLeFeCkSfmvA7p0cLrsy1Zm1g
http://foobar.website.com/page3.php&--E5WRBxuTOQikDIyBczaVXveOdRFg

Urls可能都是不同的,唯一看似静止的是&符号。 如何摆脱&符号和超越它的一切都在右边?

以下是我对上述结果的尝试:

function getresults($sterm) {
$html = file_get_html($sterm);
$result = "";
// find all span tags with class=gb1
foreach($html->find('h3[class="r"]') as $ef)
{   
$result .=  $ef->outertext . '<br>';
}
return $result;
}

function geturl($url) {
  $var = $url;
  $result = "";

preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\/url?q=\']+".
               "(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/",              

               $var, $matches);

$matches = $matches[1];

foreach($matches as $var)
{    
    $result .= $var."<br>";
}

echo preg_replace('/sa=U.*?usg=.*?AFQjCN/', "--" , $result);

}

2 个答案:

答案 0 :(得分:1)

如果url总是采用相同的格式,请使用explode:

<?php
$tmp = explode("&", "http://foobar.website.com/page3.php&--E5WRBxuTOQikDIyBczaVXveOdRFg");
?>

$ tmp [0]应该包含“http://foobar.website.com/page3.php”和 $ tmp [1]应该包含“--E5WRBxuTOQikDIyBczaVXveOdRFg”

答案 1 :(得分:0)

在&amp;之后删除所有内容的简单方法字符:

$result = substr($result, 0, strpos($result, '&'));
相关问题