Php 在Preg匹配中使用通配符

Php 在Preg匹配中使用通配符,php,regex,Php,Regex,我正在制作一个PHP刮板,下面的代码通过查看spanuiButtonText从页面中获取标题。但是,我现在想扫描超链接并使其预匹配 我想成为通配符的星星,这样即使href和onclick对每个星星都有更改,我也可以从页面中获取超链接 if(preg_match(“/(.*))/i,$cache,$matches)){print($matches[1]。“\n”);}else{} 我的完整代码: <?php $userAgent = 'Googlebot/2.1 (http://www.go

我正在制作一个PHP刮板,下面的代码通过查看span
uiButtonText
从页面中获取标题。但是,我现在想扫描超链接并使其预匹配

我想成为通配符的星星,这样即使href和onclick对每个星星都有更改,我也可以从页面中获取超链接

if(preg_match(“/(.*))/i,$cache,$matches)){print($matches[1]。“\n”);}else{}

我的完整代码:

<?php
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
$url = "http://www.facebook.com/MauiNuiBotanicalGardens/info";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$cache = $html;

if (preg_match("/<span class=\"uiButtonText\">(.*)<\/span>/i", $cache, $matches))    {print($matches[1] . "\n");}else {}
?>`
`

如果您想继续使用正则表达式,请尝试以下方法:

$html = '<span class="uiButtonText"><a href="http://google.com" class="thelink" onclick="#">Google!</a></span>';

preg_match("/<span class=\"uiButtonText\"><a href=\".*\" class=\"thelink\" onclick=\".*\">(.*)<\/a><\/span>/i", $html, $matches);

print_r($matches[1]);
$html='';
preg_match(“/并执行以下操作:

$html = file_get_html("http://www.facebook.com/MauiNuiBotanicalGardens/info");
foreach($html->find("a.thelink") as $link){
    echo $link->innertext . "<BR>";
}
$html=file\u get\u html(“http://www.facebook.com/MauiNuiBotanicalGardens/info");
foreach($html->find(“a.thelink”)作为$link){
echo$link->innertext.“
”; }

以上内容未经测试,但应能正常工作

使用DOM,而非常规表达式在地址栏中键入google.com,在PHP中搜索
DOM文档
。感谢回复,我还在学习PHP,有可能扩展一点吗?可能重复的不要撒谎,如果没有,就说你是谷歌机器人。