www.webdeveloper.com
Results 1 to 2 of 2

Thread: What wrong with this function?

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    What wrong with this function?

    I copied this coded off of a blog that I found on Google and I modified it. However, parse_images() always either returns false or a string. I want it to return an array of absolute URLs of images.

    PHP Code:
    function absolutizeUrl($u,$p){
            
    $url parse_url$u );
            
    $page parse_url$p );

            if ( 
    strpos$u '/' ) === ){
                    return 
    $u;            
            } else {
                    
    $basePath '';
                    if ( isset(
    $page['path']) && strpos(ltrim($page['path'],'/'),'/') ) {
                            
    $baseTokens explode'/'$page'path' ] );
                            
    array_pop$baseTokens ); // strip basename                     
                            
    $baseTokens[] = $u;
                            
    $u join'/'$baseTokens );
                    }
            }
            if ( ! isset( 
    $url'host' ]))
            {
                    
    $u 'http://'.$page'host' ].'/'.ltrim$u'/' );
            }
            return 
    substr$u strpos($u,'http://') );
    }

    function 
    parse_images($url){
    $ch curl_init();

    curl_setopt_array(
        
    $ch,    
        array
        (
            
    CURLOPT_URL             =>  $url,
            
    CURLOPT_RETURNTRANSFER  =>  true,
            
    CURLOPT_FOLLOWLOCATION  =>  true
        
    )
    );

    $html curl_exec($ch);

    if(!empty(
    $html)){
        
    preg_match_all('/<img[^>]*src=[\"|\'](([^\"\'\>]*)?(jpg|jpeg|gif|png))[\"|\']/Ui'$html$outPREG_PATTERN_ORDER);
        if(
    count($out[1]) < 1)
            return 
    false;
        if(
    count($out[1] == 1))
            return 
    absolutizeUrl($out[1][0],$url);
        
    $images = array();
        foreach(
    $out[1] as $image){
            
    $images[] = absolutizeUrl($image,$url);
        }
    }
    return 
    $images;
    }

    What's wrong with it?
    Last edited by narutodude000; 12-26-2010 at 12:07 AM.

  2. #2
    Join Date
    Mar 2010
    Posts
    701
    Here's what I have now. I got parse_image() to return an array, but absolutizeUrl() isn't returning absolute URLs.

    PHP Code:
    function absolutizeUrl($u,$p){
            
    $url parse_url$u );
            
    $page parse_url$p );

            if ( 
    strpos$u '/' ) === ){
                    return 
    $u;            
            } else {
                    
    $basePath '';
                    if ( isset(
    $page['path']) && strpos(ltrim($page['path'],'/'),'/') ) {
                            
    $baseTokens explode'/'$page'path' ] );
                            
    array_pop$baseTokens ); // strip basename                     
                            
    $baseTokens[] = $u;
                            
    $u join'/'$baseTokens );
                    }
            }
            if ( ! isset( 
    $url'host' ]))
            {
                    
    $u 'http://'.$page'host' ].'/'.ltrim$u'/' );
            }
            return 
    $u//substr( $u , strpos($u,'http://') );
    }

    function 
    parse_images($url){
    $ch curl_init();

    curl_setopt_array(
        
    $ch,    
        array
        (
            
    CURLOPT_URL             =>  $url,
            
    CURLOPT_RETURNTRANSFER  =>  true,
            
    CURLOPT_FOLLOWLOCATION  =>  true
        
    )
    );
    $html curl_exec($ch);

    if(!empty(
    $html)){
        
    preg_match_all('/<img[^>]*src=[\"|\'](([^\"\'\>]*)?(jpg|jpeg|gif|png))[\"|\']/Ui'$html$outPREG_PATTERN_ORDER);
        if(
    count($out[1]) < 1){
            return 
    false;
        }
        if(
    count($out[1]) == 1){
            return 
    absolutizeUrl($out[1][0],$url);
        }
        
    $images = array();
        foreach(
    $out[1] as $image){
            
    $images[] = absolutizeUrl($image,$url);
        }
    }
    return 
    $images;


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles