Skip to content

Instantly share code, notes, and snippets.

@nicoknoll
Last active December 15, 2015 07:59
Show Gist options
  • Save nicoknoll/5227414 to your computer and use it in GitHub Desktop.
Save nicoknoll/5227414 to your computer and use it in GitHub Desktop.
<?php
function replaceSrc($matches) {
global $page;
$url = $matches[1];
$img = 'images/'.basename($url);
$handle = fopen($img, 'w+');
$data = file_get_contents($url);
fwrite($handle, $data);
fclose($handle);
$page->article_images->add($img);
$page->save();
$newURL = $page->article_images->last()->url;
$return = str_replace($url, $newURL, $matches[0]);
return $return;
}
foreach(wire('pages')->get('/articles/')->children() as $page) {
$page->article_content = preg_replace_callback('/<img.*src="(.*)".*\/?>/U', "replaceSrc", $page->article_content);
$page->save();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment