dpwh11290
dpwh11290
2016-08-03 08:29

PHP从链接获取元素

已采纳

I have below code:-

$link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about

I want URL element that is - http://www.example.com/about

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • dpt1712 dpt1712 5年前
    $link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
    
    $url = explode("&url=", $link)[1];
    echo $url; //http://www.example.com/about
    
    点赞 评论 复制链接分享
  • dpq59734 dpq59734 5年前

    this will work even if url is placed somewhere else as parameter

    $link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
    $url = "";
    $params = explode("&", $link);
    for($params as $param){
        if(substr($param, 0, 4) === 'url='){
            $url = explode("=", $param)[1];
            break;
        }
    }
    echo $url; //http://www.example.com/about
    
    点赞 评论 复制链接分享
  • dongzi1209 dongzi1209 5年前
    function getSomething($url) {
        if (!(strpos($url, 'url=') !== false)) return false;
        $parse = explode('url=', $url);
        $code = $parse[1];
       return $code;
    }
    
    echo getSomething("http://www.domain.com/go.php?id=545&url=http://www.example.com/about");
    
    点赞 评论 复制链接分享
  • dragon7713 dragon7713 5年前

    You can use PHP's inbuilt parse_url and parse_str functions.

    $link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
    parse_str(parse_url($link,PHP_URL_QUERY),$url);
    echo $url['url'];
    
    点赞 评论 复制链接分享
  • doushang2571 doushang2571 5年前

    Try this:

    $link = 'http://www.domain.com/go.php?id=545&url=http://www.example.com/about';
    $uri=explode("url=",$link);
    $uri=$uri[count($uri)-1];
    
    echo $uri;
    
    点赞 评论 复制链接分享

相关推荐