


PHP regular matches the remote image address in the article and downloads the image to the local, _PHP tutorial
php regular expression matches the remote image address in the article and downloads the image to the local.
Use php regular expression to achieve this:
$content = '这里是文章内容,这里插入一张图片测试 <img src="/static/imghwm/default1.png" data-src="XXXXXXXXXXXXXXXXXXXX" class="lazy" alt="PHP regular matches the remote image address in the article and downloads the image to the local, _PHP tutorial" >'; $content = stripslashes ( $content ); $img_array = array (); // 匹配所有远程图片 preg_match_all ( "/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $img_array ); // 匹配出来的不重复图片 $img_array = array_unique ( $img_array [2] ); print_r($img_array);
The remote image is matched above, and we need to keep it locally. Two points need to be noted here:
1. Picture saving path (picture storage directory)
2. Actual access to the image address
The following is a complete example: (You can save it to the local server and modify the corresponding places for testing)
php保存远程图片到本地,php正则匹配文章中的图片地址 <?php //保存文章中远程图片到本地 //作者:yanue; // 文件保存目录路径(请更换为你自己的路径, 你可以echo一下) $save_path = $_SERVER ['DOCUMENT_ROOT'] . 'swfupload/attached/'; // 文件保存目录URL $save_url = '/swfupload/attached/'; $save_path = realpath ( $save_path ) . '/'; // 图片存储目录 $imgPath = $save_path . date ( "Ymd" ); $imgUrl = $save_url . date ( "Ymd" ); // 创建文件夹 if (! is_dir ( $imgPath )) { @mkdir ( $imgPath, 0777 ); } $content = '这里是文章内容,这里插入一张图片测试 <img src="/static/imghwm/default1.png" data-src="XXXXXXXXXXXXXXXXXXXX" class="lazy" alt="PHP regular matches the remote image address in the article and downloads the image to the local, _PHP tutorial" >'; $content = stripslashes ( $content ); $img_array = array (); // 匹配所有远程图片 preg_match_all ( "/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $img_array ); // 匹配出来的不重复图片 $img_array = array_unique ( $img_array [2] ); print_r($img_array); // 时间无限制 set_time_limit ( 0 ); foreach ( $img_array as $key => $value ) { $value = trim ( $value ); // 读取远程图片 $get_file = @file_get_contents ( $value ); // 保存到本地图片名称 $imgname = date ( "YmdHis" ) . '_' . rand ( 10000, 99999 ) . "." . substr ( $value, - 3, 3 ); // 保存到本地的实际文件地址(包含路径和名称) $fileName = $imgPath . '/' . $imgname; // 实际访问的地址 $fileurl = $imgUrl . "/" . $imgname; // 文件写入 if ($get_file) { $fp = @fopen ( $fileName, "w" ); @fwrite ( $fp, $get_file ); @fclose ( $fp ); } // 替换原来的图片地址 $content = ereg_replace ( $value, $fileurl, $content ); } echo $content; ?>
The above are the implementation techniques for PHP to match the remote image address in the article and download the image to the local. I hope it will be helpful to everyone's learning.

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
