Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zaigham/b73c0d194400e6b147bbb7bfc28bc3cd to your computer and use it in GitHub Desktop.
Save zaigham/b73c0d194400e6b147bbb7bfc28bc3cd 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