开发者

How to retrieve link location using preg_match in PHP

开发者 https://www.devze.com 2023-03-06 22:48 出处:网络
How to retrieve link location from following script using preg_match <html><head><title>Object moved</title></head><body>

How to retrieve link location from following script using preg_match

<html><head><title>Object moved</title></head><body>
<h2>Object moved to &l开发者_JAVA技巧t;a href="www.example.com">here</a>.</h2>
</body></html>

Thanks


First: if you are using CURL use

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

else

Location is in header "Location:http.."

try to get headers by get_headers or parse this output by this pattern ,

$pattern = 'href=\"(?<url>[\"]+)\"'


If I understood it correctly, you simply want to extract the url, from your html code? If so, you can do it like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消