Skip to content

Instantly share code, notes, and snippets.

@christianhanvey
Last active December 15, 2017 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save christianhanvey/5522932 to your computer and use it in GitHub Desktop.
Save christianhanvey/5522932 to your computer and use it in GitHub Desktop.
MODX utility snippet: replace absolute formed links in site content eg www.xxx.com/99 -> [[~99]] note: backing up modx_site_content table is a good idea before running this :) (original author: BobRay)
<?php
$docs = $modx->getCollection('modDocument');
$pattern = '/www.xxx.com/(\d+)/';
$replacement = '[[~$1]]';
$count = 0;
foreach ($docs as $doc) {
$content = $doc->getContent();
$hash1 = sha1($content);
$content = preg_replace($pattern, $replacement, $content);
$hash2 = sha1($content);
if ($hash1 === $hash2) { /* no change */.
continue;
}
$doc->setContent($content);
$doc->save();
$count++;
}
return 'Modified ' . $count . ' Resources'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment