2012-04-03 34 views
-4
这个PHP代码

我找了vb.net相当于该功能的:我没有PHP的经验,但过这个代码这显然给你从谷歌图像随机图像URL来什么是在VB.net

function GetRandomImageURL($topic='', $min=0, $max=100) 
{ 
    // get random image from Google 
    if ($topic=='') $topic='image'; 
    $ofs=mt_rand($min, $max); 
    $geturl='http://www.google.ca/images?q=' . $topic . '&start=' . $ofs . '&gbv=1'; 
    $data=file_get_contents($geturl); 

    $f1='<div id="center_col">'; 
    $f2='<a href="/imgres?imgurl='; 
    $f3='&amp;imgrefurl='; 

    $pos1=strpos($data, $f1)+strlen($f1); 
    if ($pos1==FALSE) return FALSE; 
    $pos2=strpos($data, $f2, $pos1)+strlen($f2); 
    if ($pos2==FALSE) return FALSE; 
    $pos3=strpos($data, $f3, $pos2); 
    if ($pos3==FALSE) return FALSE; 
    return substr($data, $pos2, $pos3-$pos2); 
} 

回答

1

它主要是字符串操作,建立图像url。它选择在这条线上指数0100之间的伪随机图像

$ofs=mt_rand($min, $max); 

同样可以与Random类在.net实现

Dim rnd As New Random() 
Dim ofs As Int = rnd.Next(min, max) 

我离开这个字符串操作到OP,但其可能更好的做法是使用StringBuilder课程,或者使用String.Format

值得考虑的是0100的界限是否过于谨慎。