php 正则字符串中外部链接代码函数

 更新时间:2016年11月25日 16:28  点击:1622
 代码如下 复制代码

function deleteemptyarray( $val )
{
 $links ='';
 if( is_array( $val ) )
 {
  foreach( $val as $v =>$_v)
  {
   if( !empty( $_v[0] ) )
   {
    $links .=$_v[0].'|';
   }
  }  
  return substr($links,0,-1);
 }
 else
 {
  return false;
 }
}

function getoutlink($body)
{
 
 $tempcontent = $body;
 preg_match_all("/<a(.*?)href=(.*?)</a>/i",$tempcontent,$tempurl);
 $urls =array();
 foreach($tempurl[0] as $value)
 {
  if(strstr($value,'http') )
  {
   if(stristr($value ,$localurl))
   {
    continue;
   }
   else
   {    
    preg_match_all("/hrefs*=s*(['"]?)(.*?)\1/is", $value, $vlink);    
    $urls[] = $vlink[2];
   }
  }
 }
 //print_r($urls);
 $strurl = explode('|',deleteemptyarray( $urls));
 $tempcount = array_unique($strurl);
 echo '&nbsp;外链'.count($tempcount)-1;
}

//调用方法

 代码如下 复制代码
$body ="<a href=/ab.htm>aaa</a><a href=http://www.111cn.net>我是外部连接</a>";
echo getoutlink($body);

打开php.ini,首先找到
;;;;;;;;;;;;;;;;
; file uploads ;
;;;;;;;;;;;;;;;;
区域,有影响文件上传的以下几个参数:

file_uploads   =   on   ;是否允许通过http上传文件的开关。默认为on即是开

upload_tmp_dir   ;文件上传至服务器上存储临时文件的地方,如果没指定就会用系统默认的临时文件夹

upload_max_filesize   =   8m   ;望文生意,即允许上传文件大小的最大值。默认为2m



;;;;;;;;;;;;;;;;;
; data handling ;
;;;;;;;;;;;;;;;;;
区域,还有一项:

post_max_size   =   8m ;指通过表单post给php的所能接收的最大值,包括表单里的所有值。默认为8m


一般地,设置好上述四个参数后,上传<=8m的文件是不成问题,在网络正常的情况下。


但如果要上传>8m的大体积文件,只设置上述四项还一定能行的通。除非你的网络真有100m/s的上传高速,否则你还得关心关心下面的参数:

;;;;;;;;;;;;;;;;;;;
; resource limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time   =   600 ;每个php页面运行的最大时间值(秒),默认30秒

max_input_time = 600 ;每个php页面接收数据所需的最大时间,默认60秒

memory_limit   =   8m   ;每个php页面所吃掉的最大内存,默认8m

 代码如下 复制代码

function imagebmp($img,$file="",$rle=0)
{


$colorcount=imagecolorstotal($img);

$transparent=imagecolortransparent($img);
$istransparent=$transparent!=-1;


if($istransparent) $colorcount--;

if($colorcount==0) {$colorcount=0; $bitcount=24;};
if(($colorcount>0)and($colorcount<=2)) {$colorcount=2; $bitcount=1;};
if(($colorcount>2)and($colorcount<=16)) { $colorcount=16; $bitcount=4;};
if(($colorcount>16)and($colorcount<=256)) { $colorcount=0; $bitcount=8;};


                $width=imagesx($img);
                $height=imagesy($img);

                $zbytek=(4-($width/(8/$bitcount))%4)%4;

                if($bitcount<24) $palsize=pow(2,$bitcount)*4;

                $size=(floor($width/(8/$bitcount))+$zbytek)*$height+54;
                $size+=$palsize;
                $offset=54+$palsize;

                // bitmap file header
                $ret = 'bm';                        // header (2b)
                $ret .= int_to_dword($size);        // size of file (4b)
                $ret .= int_to_dword(0);        // reserved (4b)
                $ret .= int_to_dword($offset);        // byte location in the file which is first byte of image (4b)
                // bitmap info header
                $ret .= int_to_dword(40);        // size of bitmapinfoheader (4b)
                $ret .= int_to_dword($width);        // width of bitmap (4b)
                $ret .= int_to_dword($height);        // height of bitmap (4b)
                $ret .= int_to_word(1);        // biplanes = 1 (2b)
                $ret .= int_to_word($bitcount);        // bibitcount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 mil)} (2b)
                $ret .= int_to_dword($rle);        // rle compression (4b)
                $ret .= int_to_dword(0);        // width x height (4b)
                $ret .= int_to_dword(0);        // bixpelspermeter (4b)
                $ret .= int_to_dword(0);        // biypelspermeter (4b)
                $ret .= int_to_dword(0);        // number of palettes used (4b)
                $ret .= int_to_dword(0);        // number of important colour (4b)
                // image data

                $cc=$colorcount;
                $sl1=strlen($ret);
                if($cc==0) $cc=256;
                if($bitcount<24)
                   {
                    $colortotal=imagecolorstotal($img);
                     if($istransparent) $colortotal--;

                     for($p=0;$p<$colortotal;$p++)
                     {
                      $color=imagecolorsforindex($img,$p);
                       $ret.=inttobyte($color["blue"]);
                       $ret.=inttobyte($color["green"]);
                       $ret.=inttobyte($color["red"]);
                       $ret.=inttobyte(0); //reserved
                     };

                    $ct=$colortotal;
                  for($p=$colortotal;$p<$cc;$p++)
                       {
                      $ret.=inttobyte(0);
                      $ret.=inttobyte(0);
                      $ret.=inttobyte(0);
                      $ret.=inttobyte(0); //reserved
                     };
                   };


if($bitcount<=8)
{

 for($y=$height-1;$y>=0;$y--)
 {
  $bwrite="";
  for($x=0;$x<$width;$x++)
   {
   $color=imagecolorat($img,$x,$y);
   $bwrite.=decbinx($color,$bitcount);
   if(strlen($bwrite)==8)
    {
     $retd.=inttobyte(bindec($bwrite));
     $bwrite="";
    };
   };

  if((strlen($bwrite)<8)and(strlen($bwrite)!=0))
    {
     $sl=strlen($bwrite);
     for($t=0;$t<8-$sl;$t++)
      $sl.="0";
     $retd.=inttobyte(bindec($bwrite));
    };
 for($z=0;$z<$zbytek;$z++)
   $retd.=inttobyte(0);
 };
};

if(($rle==1)and($bitcount==8))
{
 for($t=0;$t<strlen($retd);$t+=4)
  {
   if($t!=0)
   if(($t)%$width==0)
    $ret.=chr(0).chr(0);

   if(($t+5)%$width==0)
   {
     $ret.=chr(0).chr(5).substr($retd,$t,5).chr(0);
     $t+=1;
   }
   if(($t+6)%$width==0)
    {
     $ret.=chr(0).chr(6).substr($retd,$t,6);
     $t+=2;
    }
    else
    {
     $ret.=chr(0).chr(4).substr($retd,$t,4);
    };
  };
  $ret.=chr(0).chr(1);
}
else
{
$ret.=$retd;
};


                if($bitcount==24)
                {
                for($z=0;$z<$zbytek;$z++)
                 $dopl.=chr(0);

                for($y=$height-1;$y>=0;$y--)
                 {
                 for($x=0;$x<$width;$x++)
                        {
                           $color=imagecolorsforindex($img,imagecolorat($img,$x,$y));
                           $ret.=chr($color["blue"]).chr($color["green"]).chr($color["red"]);
                        }
                 $ret.=$dopl;
                 };

                 };

  if($file!="")
   {
    $r=($f=fopen($file,"w"));
    $r=$r and fwrite($f,$ret);
    $r=$r and fclose($f);
    return $r;
   }
  else
  {
   echo $ret;
  };
};


/*
*------------------------------------------------------------
*                    imagecreatefrombmp
*------------------------------------------------------------
*            - reads image from a bmp file
*
*         parameters:  $file - target file to load
*
*            returns: image id
*/

function imagecreatefrombmp($file)
{
global  $currentbit, $echomode;

$f=fopen($file,"r");
$header=fread($f,2);

if($header=="bm")
{
 $size=freaddword($f);
 $reserved1=freadword($f);
 $reserved2=freadword($f);
 $firstbyteofimage=freaddword($f);

 $sizebitmapinfoheader=freaddword($f);
 $width=freaddword($f);
 $height=freaddword($f);
 $biplanes=freadword($f);
 $bibitcount=freadword($f);
 $rlecompression=freaddword($f);
 $widthxheight=freaddword($f);
 $bixpelspermeter=freaddword($f);
 $biypelspermeter=freaddword($f);
 $numberofpalettesused=freaddword($f);
 $numberofimportantcolors=freaddword($f);

if($bibitcount<24)
 {
  $img=imagecreate($width,$height);
  $colors=pow(2,$bibitcount);
  for($p=0;$p<$colors;$p++)
   {
    $b=freadbyte($f);
    $g=freadbyte($f);
    $r=freadbyte($f);
    $reserved=freadbyte($f);
    $palette[]=imagecolorallocate($img,$r,$g,$b);
   };

 


if($rlecompression==0)
{
   $zbytek=(4-ceil(($width/(8/$bibitcount)))%4)%4;

for($y=$height-1;$y>=0;$y--)
    {
     $currentbit=0;
     for($x=0;$x<$width;$x++)
      {
         $c=freadbits($f,$bibitcount);
       imagesetpixel($img,$x,$y,$palette[$c]);
      };
    if($currentbit!=0) {freadbyte($f);};
    for($g=0;$g<$zbytek;$g++)
     freadbyte($f);
     };

 };
};


if($rlecompression==1) //$bi_rle8
{
$y=$height;

$pocetb=0;

while(true)
{
$y--;
$prefix=freadbyte($f);
$suffix=freadbyte($f);
$pocetb+=2;

$echoit=false;

if($echoit)echo "prefix: $prefix suffix: $suffix<br>";
if(($prefix==0)and($suffix==1)) break;
if(feof($f)) break;

while(!(($prefix==0)and($suffix==0)))
{
 if($prefix==0)
  {
   $pocet=$suffix;
   $data.=fread($f,$pocet);
   $pocetb+=$pocet;
   if($pocetb%2==1) {freadbyte($f); $pocetb++;};
  };
 if($prefix>0)
  {
   $pocet=$prefix;
   for($r=0;$r<$pocet;$r++)
    $data.=chr($suffix);
  };
 $prefix=freadbyte($f);
 $suffix=freadbyte($f);
 $pocetb+=2;
 if($echoit) echo "prefix: $prefix suffix: $suffix<br>";
};

for($x=0;$x<strlen($data);$x++)
 {
  imagesetpixel($img,$x,$y,$palette[ord($data[$x])]);
 };
$data="";

};

};


if($rlecompression==2) //$bi_rle4
{
$y=$height;
$pocetb=0;

/*while(!feof($f))
 echo freadbyte($f)."_".freadbyte($f)."<br>";*/
while(true)
{
//break;
$y--;
$prefix=freadbyte($f);
$suffix=freadbyte($f);
$pocetb+=2;

$echoit=false;

if($echoit)echo "prefix: $prefix suffix: $suffix<br>";
if(($prefix==0)and($suffix==1)) break;
if(feof($f)) break;

while(!(($prefix==0)and($suffix==0)))
{
 if($prefix==0)
  {
   $pocet=$suffix;

   $currentbit=0;
   for($h=0;$h<$pocet;$h++)
    $data.=chr(freadbits($f,4));
   if($currentbit!=0) freadbits($f,4);
   $pocetb+=ceil(($pocet/2));
   if($pocetb%2==1) {freadbyte($f); $pocetb++;};
  };
 if($prefix>0)
  {
   $pocet=$prefix;
   $i=0;
   for($r=0;$r<$pocet;$r++)
    {
    if($i%2==0)
     {
      $data.=chr($suffix%16);
     }
     else
     {
      $data.=chr(floor($suffix/16));
     };
    $i++;
    };
  };
 $prefix=freadbyte($f);
 $suffix=freadbyte($f);
 $pocetb+=2;
 if($echoit) echo "prefix: $prefix suffix: $suffix<br>";
};

for($x=0;$x<strlen($data);$x++)
 {
  imagesetpixel($img,$x,$y,$palette[ord($data[$x])]);
 };
$data="";

};

};


 if($bibitcount==24)
{
 $img=imagecreatetruecolor($width,$height);
 $zbytek=$width%4;

   for($y=$height-1;$y>=0;$y--)
    {
     for($x=0;$x<$width;$x++)
      {
       $b=freadbyte($f);
       $g=freadbyte($f);
       $r=freadbyte($f);
       $color=imagecolorexact($img,$r,$g,$b);
       if($color==-1) $color=imagecolorallocate($img,$r,$g,$b);
       imagesetpixel($img,$x,$y,$color);
      }
    for($z=0;$z<$zbytek;$z++)
     freadbyte($f);
   };
};
return $img;

};


fclose($f);


};

 

 

/*
* helping functions:
*-------------------------
*
* freadbyte($file) - reads 1 byte from $file
* freadword($file) - reads 2 bytes (1 word) from $file
* freaddword($file) - reads 4 bytes (1 dword) from $file
* freadlngint($file) - same as freaddword($file)
* decbin8($d) - returns binary string of d zero filled to 8
* retbits($byte,$start,$len) - returns bits $start->$start+$len from $byte
* freadbits($file,$count) - reads next $count bits from $file
* rgbtohex($r,$g,$b) - convert $r, $g, $b to hex
* int_to_dword($n) - returns 4 byte representation of $n
* int_to_word($n) - returns 2 byte representation of $n
*/

function freadbyte($f)
{
 return ord(fread($f,1));
};

function freadword($f)
{
 $b1=freadbyte($f);
 $b2=freadbyte($f);
 return $b2*256+$b1;
};


function freadlngint($f)
{
return freaddword($f);
};

function freaddword($f)
{
 $b1=freadword($f);
 $b2=freadword($f);
 return $b2*65536+$b1;
};

 

function retbits($byte,$start,$len)
{
$bin=decbin8($byte);
$r=bindec(substr($bin,$start,$len));
return $r;

};

 

$currentbit=0;
function freadbits($f,$count)
{
 global $currentbit,$smode;
 $byte=freadbyte($f);
 $lastcbit=$currentbit;
 $currentbit+=$count;
 if($currentbit==8)
  {
   $currentbit=0;
  }
 else
  {
   fseek($f,ftell($f)-1);
  };
 return retbits($byte,$lastcbit,$count);
};

 

function rgbtohex($red,$green,$blue)
  {
   $hred=dechex($red);if(strlen($hred)==1) $hred="0$hred";
   $hgreen=dechex($green);if(strlen($hgreen)==1) $hgreen="0$hgreen";
   $hblue=dechex($blue);if(strlen($hblue)==1) $hblue="0$hblue";
   return($hred.$hgreen.$hblue);
  };

        function int_to_dword($n)
        {
                return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24) & 255);
        }
        function int_to_word($n)
        {
                return chr($n & 255).chr(($n >> 8) & 255);
        }


function decbin8($d)
{
return decbinx($d,8);
};

function decbinx($d,$n)
{
$bin=decbin($d);
$sbin=strlen($bin);
for($j=0;$j<$n-$sbin;$j++)
 $bin="0$bin";
return $bin;
};

function inttobyte($n)
{
return chr($n);
};

//实例方法

 代码如下 复制代码
include_once('bmp.php');
$image=imagecreatefrombmp('a.bmp');
imagejpeg($image,'a.jpeg');
imagedestroy($image);

 

fck 上传图片增加水印代码修改 //我们只要找到editor/filemanger/conectors/php/commands.php文件把下面这段php代码替换成你的commands.php文件就行了。

function getfolders( $resourcetype, $currentfolder )
{
 // map the virtual path to the local server path.
 $sserverdir = servermapfolder( $resourcetype, $currentfolder, 'getfolders' ) ;

 // array that will hold the folders names.
 $afolders = array() ;

 $ocurrentfolder = opendir( $sserverdir ) ;

 while ( $sfile = readdir( $ocurrentfolder ) )
 {
  if ( $sfile != '.' && $sfile != '..' && is_dir( $sserverdir . $sfile ) )
   $afolders[] = '<folder name="' . converttoxmlattribute( $sfile ) . '" />' ;
 }

 closedir( $ocurrentfolder ) ;

 // open the "folders" node.
 echo "<folders>" ;

 natcasesort( $afolders ) ;
 foreach ( $afolders as $sfolder )
  echo $sfolder ;

 // close the "folders" node.
 echo "</folders>" ;
}

function getfoldersandfiles( $resourcetype, $currentfolder )
{
 // map the virtual path to the local server path.
 $sserverdir = servermapfolder( $resourcetype, $currentfolder, 'getfoldersandfiles' ) ;

 // arrays that will hold the folders and files names.
 $afolders = array() ;
 $afiles  = array() ;

 $ocurrentfolder = opendir( $sserverdir ) ;

 while ( $sfile = readdir( $ocurrentfolder ) )
 {
  if ( $sfile != '.' && $sfile != '..' )
  {
   if ( is_dir( $sserverdir . $sfile ) )
    $afolders[] = '<folder name="' . converttoxmlattribute( $sfile ) . '" />' ;
   else
   {
    $ifilesize = @filesize( $sserverdir . $sfile ) ;
    if ( !$ifilesize ) {
     $ifilesize = 0 ;
    }
    if ( $ifilesize > 0 )
    {
     $ifilesize = round( $ifilesize / 1024 ) ;
     if ( $ifilesize < 1 ) $ifilesize = 1 ;
    }

    $afiles[] = '<file name="' . converttoxmlattribute( $sfile ) . '" size="' . $ifilesize . '" />' ;
   }
  }
 }

 // send the folders
 natcasesort( $afolders ) ;
 echo '<folders>' ;

 foreach ( $afolders as $sfolder )
  echo $sfolder ;

 echo '</folders>' ;

 // send the files
 natcasesort( $afiles ) ;
 echo '<files>' ;

 foreach ( $afiles as $sfiles )
  echo $sfiles ;

 echo '</files>' ;
}

function createfolder( $resourcetype, $currentfolder )
{
 if (!isset($_get)) {
  global $_get;
 }
 $serrornumber = '0' ;
 $serrormsg  = '' ;

 if ( isset( $_get['newfoldername'] ) )
 {
  $snewfoldername = $_get['newfoldername'] ;
  $snewfoldername = sanitizefoldername( $snewfoldername ) ;

  if ( strpos( $snewfoldername, '..' ) !== false )
   $serrornumber = '102' ;  // invalid folder name.
  else
  {
   // map the virtual path to the local server path of the current folder.
   $sserverdir = servermapfolder( $resourcetype, $currentfolder, 'createfolder' ) ;

   if ( is_writable( $sserverdir ) )
   {
    $sserverdir .= $snewfoldername ;

    $serrormsg = createserverfolder( $sserverdir ) ;

    switch ( $serrormsg )
    {
     case '' :
      $serrornumber = '0' ;
      break ;
     case 'invalid argument' :
     case 'no such file or directory' :
      $serrornumber = '102' ;  // path too long.
      break ;
     default :
      $serrornumber = '110' ;
      break ;
    }
   }
   else
    $serrornumber = '103' ;
  }
 }
 else
  $serrornumber = '102' ;

 // create the "error" node.
 echo '<error number="' . $serrornumber . '" originaldescription="' . converttoxmlattribute( $serrormsg ) . '" />' ;
}

function fileupload( $resourcetype, $currentfolder, $scommand )
{
 if (!isset($_files)) {
  global $_files;
 }
 $serrornumber = '0' ;
 $sfilename = '' ;

 if ( isset( $_files['newfile'] ) && !is_null( $_files['newfile']['tmp_name'] ) )
 {
  global $config ;

  $ofile = $_files['newfile'] ;

  // map the virtual path to the local server path.
  $sserverdir = servermapfolder( $resourcetype, $currentfolder, $scommand ) ;

  // get the uploaded file name.
  $sfilename = $ofile['name'] ;
  $sfilename = sanitizefilename( $sfilename ) ;

  $soriginalfilename = $sfilename ;

  // get the extension.
  $sextension = substr( $sfilename, ( strrpos($sfilename, '.') + 1 ) ) ;
  $sextension = strtolower( $sextension ) ;

  if ( isset( $config['secureimageuploads'] ) )
  {
   if ( ( $isimagevalid = isimagevalid( $ofile['tmp_name'], $sextension ) ) === false )
   {
    $serrornumber = '202' ;
   }
  }

  if ( isset( $config['htmlextensions'] ) )
  {
   if ( !ishtmlextension( $sextension, $config['htmlextensions'] ) &&
    ( $detecthtml = detecthtml( $ofile['tmp_name'] ) ) === true )
   {
    $serrornumber = '202' ;
   }
  }

  // check if it is an allowed extension.
  if ( !$serrornumber && isallowedext( $sextension, $resourcetype ) )
  {
   $icounter = 0 ;

   while ( true )
   {
    $sfilepath = $sserverdir . $sfilename ;
    //$sfilepath =$sserverdir.time().$sextension;
    

    if ( is_file( $sfilepath ) )
    {
     $icounter++ ;
     $sfilename = removeextension( $soriginalfilename ) . '(' . $icounter . ').' . $sextension ;
     $serrornumber = '201' ;
    }
    else
    {
     move_uploaded_file( $ofile['tmp_name'], $sfilepath ) ;

     if ( is_file( $sfilepath ) )
     {
      if ( isset( $config['chmodonupload'] ) && !$config['chmodonupload'] )
      {
       break ;
      }

      $permissions = 0777;

      if ( isset( $config['chmodonupload'] ) && $config['chmodonupload'] )
      {
       $permissions = $config['chmodonupload'] ;
      }

      $oldumask = umask(0) ;
      chmod( $sfilepath, $permissions ) ;
      umask( $oldumask ) ;
     }

     break ;
    }
   }
   
  

 代码如下 复制代码
 //增加水印
   // add start
   
    
   $waterimg="111cn.net.png";    //水印图片  这里修改你的水印图片所在的地址
    
   if($watermark==1)
   {
    $image_size = getimagesize($sfilepath);
    $iinfo=getimagesize($sfilepath,$iinfo);
    $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
    $white=imagecolorallocate($nimage,255,255,255);
    $black=imagecolorallocate($nimage,0,0,0);
    $red=imagecolorallocate($nimage,255,0,0);
    imagefill($nimage,0,0,$white);
    switch ($iinfo[2])
    {
     case 1:
      $simage =imagecreatefromgif($sfilepath);
      break;
     case 2:
      $simage =imagecreatefromjpeg($sfilepath);
      break;
     case 3:
      $simage =imagecreatefrompng($sfilepath);
      break;
   //            case 6:
   //            $simage =imagecreatefromwbmp($sfilepath);
   //            break;
     default:
      die("不支持的文件类型");
      exit;
    }
    
    imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
    
    switch($watertype)
    {
     case 1:   //加水印字符串
      imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
      break;
     case 2:   //加水印图片
      $simage1 =imagecreatefrompng($waterimg);
      imagecopy($nimage,$simage1,$image_size[0]/2,$image_size[0]/2,0,0,240,65);
      imagedestroy($simage1);
      break;
    }
         
    switch ($iinfo[2])
    {
     case 1:
      imagegif($nimage, $sfilepath);
   //            imagejpeg($nimage, $sfilepath);
      break;
     case 2:
      imagejpeg($nimage, $sfilepath);
      break;
     case 3:
      imagepng($nimage, $sfilepath);
      break;
   //            case 6:
   //            imagewbmp($nimage, $sfilepath);
   //            //imagejpeg($nimage, $sfilepath);
   //            break;
    }
    
    //覆盖原上传文件
    imagedestroy($nimage);
    imagedestroy($simage);
   }


  //增加水印结果
   
   if ( file_exists( $sfilepath ) )
   {
    //previous checks failed, try once again
    if ( isset( $isimagevalid ) && $isimagevalid === -1 && isimagevalid( $sfilepath, $sextension ) === false )
    {
     @unlink( $sfilepath ) ;
     $serrornumber = '202' ;
    }
    else if ( isset( $detecthtml ) && $detecthtml === -1 && detecthtml( $sfilepath ) === true )
    {
     @unlink( $sfilepath ) ;
     $serrornumber = '202' ;
    }
   }
  }
  else
   $serrornumber = '202' ;
 }
 else
  $serrornumber = '202' ;


 $sfileurl = combinepaths( getresourcetypepath( $resourcetype, $scommand ) , $currentfolder ) ;
 $sfileurl = combinepaths( $sfileurl, $sfilename ) ;

 senduploadresults( $serrornumber, $sfileurl, $sfilename ) ;

 exit ;
}?>

 代码如下 复制代码

function my_file_get_contents($url, $timeout=30) {
 if ( function_exists('curl_init') ) 
 {
  $ch = curl_init();
  curl_setopt ($ch, curlopt_url, $url);
  curl_setopt ($ch, curlopt_returntransfer, 1);
  curl_setopt ($ch, curlopt_connecttimeout, $timeout);
  $file_contents = curl_exec($ch);
  curl_close($ch);
 }
 else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )   
 {
  $file_contents = @file_get_contents($url);
 }
 else
 {
  $file_contents = '';
 }
 return $file_contents;
}


 

 代码如下 复制代码

function get_remote($body,$title){

 $img_array = array(); 
 $img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集远程图片保存地址
 //die($img_path);
 $img_rpath='/upfile/news/'.date("y/m/d/");  //设置访问地址
 $body = stripslashes(strtolower($body)); 
 preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array); 
 $img_array = array_unique($img_array[2]); 
 foreach ($img_array as $key => $value) {
  $get_file = my_file_get_contents($value,60);
  $filetime = time();   
  $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);
  if(empty($get_file)){
   @sleep(10);
   $get_file = my_file_get_contents($value,30);
   if(empty($get_file)){
    $body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
    continue;
    }
  }
  if(!empty($get_file) ){
   if( mkdirs($img_path) )
   {
    $fp = fopen($img_path.$filename,"w");
    if(fwrite($fp,$get_file)){         
     $body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);
    }
    fclose($fp);
    @sleep(6);
   }   
  }    
 
 }
 $body =str_replace("<img","<img ",$body); 
 return $body;
 
}

function mkdirs($dir)
{
 if(!is_dir($dir)){
  if(!mkdirs(dirname($dir))){
   return false;}
  if(!mkdir($dir,0777)){
   return false;}
 }
 return true;
}

$str ='fasfsdafsa<img src=http://filesimg.111cn.net/2010/03/2010062300391582.jpg />';
echo get_remote($str,'图片');

本站原创教程转请注明来源,文件上传代码会这样做。

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • c#中判断字符串是不是数字或字母的方法

    这篇文章介绍了C#判断字符串是否数字或字母的实例,有需要的朋友可以参考一下...2020-06-25
  • PostgreSQL判断字符串是否包含目标字符串的多种方法

    这篇文章主要介绍了PostgreSQL判断字符串是否包含目标字符串的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • php字符串按照单词逐个进行反转的方法

    本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下:下面的php代码可以将字符串按照单词进行反转输出,实际上是现将字符串按照空格分隔到数组,然后对数组进行反转输出。...2015-03-15
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • 使用list stream: 任意对象List拼接字符串

    这篇文章主要介绍了使用list stream:任意对象List拼接字符串操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-09
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • MySQL 字符串拆分操作(含分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • C# 16 进制字符串转 int的方法

    这篇文章主要介绍了C# 16 进制字符串转 int的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28