preg match - 如何在 PHP 中使用 preg_match 检索链接位置


preg match - How to retrieve link location using preg_match in PHP

如何使用preg_match从以下脚本中检索链接位置

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="www.example.com">here</a>.</h2>
</body></html>

谢谢

首先:如果您使用的是 CURL,请使用

    <?php 
// ....
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION) 
    ?>

位置

位于标题"位置:http.."

尝试通过get_headers获取标头或者通过这个模式解析这个输出,

$pattern = 'href='"(?<url>['"]+)'"'
如果我

理解正确,您只想从您的 html 代码中提取 url?如果是这样,您可以这样做:

preg_match('/object moved to <a href="(.+?)"> here/,$html_text,$match');
$url = $match[1];