Hi everyone,
First off, great conference earlier this week! It inspired me to work on my first plugin. This will be of limited use to the community, but I’ll throw the code out there anyway in case anyone can benefit from it.
Basically, I have several hundred pages that contain course codes, like the muted-blue links on this page:
http://www2.ku.edu/~distinction/cgi-bin/degree-requirements64
Originally, these were just text and not links, but it would’ve been extremely tedious for our content editors to manually link every course on the site. My plugin fires at OnWebPagePrerender, and using some regular expression awesomeness converts the text into a link to a corresponding page on our registration system. The plugin looks like this (there’s some other stuff going on there to prevent this plugin from affecting every single page, but the RegEx line is my real issue):
$e = &$modx->event;
if ($e->name == 'OnWebPagePrerender') {
$docid = $modx->documentIdentifier;
$pagetitle = $modx->documentObject['pagetitle'];
$tvnames = array('coursereqs_check');
$tvoutput = $modx->getTemplateVarOutput($tvnames, $docid, 1);
$coursereqs_check = $tvoutput['coursereqs_check'];
if (($coursereqs_check == 1) || ($pagetitle == "Degree Requirements") || ($pagetitle == "Minor Requirements") || ($pagetitle == "Requirements")) {
$o = &$modx->documentOutput; // get a reference of the output
$o = preg_replace("([A-Z]{2,4}\s[0-9]{3})", "<a href=\"https://classes.ku.edu/Classes/CourseSearchAPI.action?classesSearchText=$0\">$0</a>",$o);
}
}
$e->output($o);
return '';
As you can tell, the RegEx looks for an all-caps, 2-4 letter string followed by a space and a three-digit number. This works great, except for one thing: some of the course subjects contain html-encoded ampersands (&), like on this page:
http://www2.ku.edu/~distinction/cgi-bin/degree-requirements34
This screws up my RegEx logic entirely. I’m wondering if I need to unencode them first before running them through preg_replace and then re-encode at the end, but that doesn’t seem to be working. Can anyone help?
Thanks!