Home > Article > Backend Development > PHP regular match the remote image address in the article and download the image to the local_PHP tutorial
Today I am doing a simple collection program that requires downloading the content of the other party's website, and then saving the pictures in the content on the local server. Now I will introduce to you my specific operation method. Downloading pictures mainly uses the file_get_contents function. The specific method as follows.
Here we use PHP regular expressions to achieve:
The code is as follows
|
Copy code
|
||||
$content = 'Here is the content of the article, insert a picture here to test'; $content = stripslashes ( $content ); $img_array = array ();// Match all remotes Picture preg_match_all ( "/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $img_array );// Matched unique images $img_array = array_unique ( $img_array [2] );
|
The code is as follows | Copy code |