Created
October 26, 2016 13:21
-
-
Save fdcore/7010d5129e7f1fdf29976a0a3416d43d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
@author Dmitriy Nyashkin | |
*/ | |
$folder_path="static/"; // путь куда ложить картинки | |
$folder_public_url="https://localhost/static/"; // путь на который заменить | |
function download_file($url, $folder, $filename){ | |
$ext = explode('.', $url); | |
$counter = 1; | |
$filename_save = $filename.'-'.$counter.'.'.end($ext); | |
$path = $folder.$filename_save; | |
while (file_exists($path)){ | |
$counter++; | |
$filename_save = $filename.'-'.$counter.'.'.end($ext); | |
$path = $folder.$filename_save; | |
} | |
if(!file_exists($folder.$filename_save)){ | |
$content = @file_get_contents($url); | |
if($content) | |
@file_put_contents($folder.$filename_save, $content); | |
return $filename_save; | |
} | |
return false; | |
} | |
/* | |
$content контент в котором искать ссылки | |
$folder локальный путь куда сохранять файлы | |
$folder_public_url путь на который заменить в тексте | |
$default_file_name префикс имени файла | |
@return обработанный контент | |
*/ | |
function parse_text_with_images($content, $folder, $folder_public_url, $default_file_name='') | |
{ | |
preg_match_all('/(https?:.[^ ]+\.(jpg|jpeg|png))/i', $content, $match); | |
if(isset($match[1])){ | |
$links = $match[1]; | |
foreach ($links as $value) { | |
$filename = download_file($value, $folder, $default_file_name); | |
$content = str_replace($value, $folder_public_url.$filename, $content); | |
} | |
} | |
return $content; | |
} | |
echo parse_text_with_images('пример ссылки https://pp.vk.me/c837629/v837629689/8c37/0l8PFjkKc9A.jpg пример ссылки | |
не на картинку | |
https://vk.com/fdcore_nyashkin ещё ссылка https://cs7055.vk.me/c836426/v836426941/334d/bn5W3qTK9ko.jpg | |
и PNG http://nyashk.in/assets/img/logo.png', | |
$folder_path, | |
$folder_public_url, | |
'news-'.date('ymd') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment