We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    I have a site that’s pulling in an RSS feed using FeedX. The ampersands in the feed’s links are not properly encoded (they just use & instead of &), which is screwing up my validation. Unfortunately, I don’t have access to edit the feed itself and fix this, so I’m wondering if there’s a way (probably with a plug-in?) that will let me properly encode things on the fly as the page is being rendered.

    I have seen a plug-in in the repository called &xhtml, but I can’t get it to work properly, and the documentation is pretty sparse. If this is the correct plug-in, could someone please walk me through setting it up and getting it to work?

    Thanks!
      • 26931
      • 2,314 Posts
      i couldn’t find it in the repository...

      i use this:
      //<?php
      // Begin plugin code
      $e = &$modx->event;
      
      $id = $modx->event->params['id'];
      
      if ($e->name == 'OnWebPagePrerender') {
      	$o = &$modx->documentOutput; // get a reference of the output
      	// Replace all ^text inside hats^ to <sup> tags, useful for more friendly Wayfinder replacements
      	$o = preg_replace('%\^([()a-zA-Z0-9 ]*)\^%i', "<sup>$1</sup>",$o);
      	// Replace orphan & with &
      	$o = preg_replace('%&(?![a-zA-Z0-9#]{1,6})%i', "&",$o);
      	// Replace ].]> with ]]>, useful for CDATA tags inside PHx calls: http://modxcms.com/forums/index.php?topic=31435.0
      	$o = str_replace('/* ].]> */', '/* ]]> */',$o);
      }
      
      $e->output($o);
      return '';

      check in sytem events: OnWebPagePrerender

      j
        • 813 ☆ A M B ☆
        • 326 Posts
        For some reason, this still didn’t work. Could it be a problem with the feed itself, or maybe a conflict between this and FeedX?

        Oh, and here is the link to &xhtml (also didn’t work):

        http://modxcms.com/extras/package/?package=1
          • 813 ☆ A M B ☆
          • 326 Posts
          Oh wait -- I somehow got &xhtml working (I think I had the wrong event checked before). Not sure why your other suggestion didn’t work, but I appreciate the help!
            • 15175
            • 116 Posts
            I copied and pasted the code, and checked ’OnWebPagePrerender’. Unfortunately it is having no effect on my ampersands.

            EDIT.

            I should have looked at it a bit closer - it says "orphan &". It does work for these, but not if the ampersand is within a string such as a URL.

              • 26931
              • 2,314 Posts
              I copied and pasted the code, and checked ’OnWebPagePrerender’. Unfortunately it is having no effect on my ampersands.
              make sure its not wrapped in <?php ?>
                • 15175
                • 116 Posts
                Hi Sharkbait.

                Actually it is working fine for for the orphaned ampersands as it says in the plug-in. I was wanting it to replace all ampersands, including those that are within a string. It could be a good reason for me to learn a bit more about regular expressions.
                  • 16278
                  • 928 Posts
                  The last part of the code for the API makeURL function may be of interest if you’re diving into regular expressions:
                          if ($this->config['xhtml_urls']) {return preg_replace("/&(?!amp;)/","&", $host . $virtualDir . $url);}

                  laugh KP
                    • 15175
                    • 116 Posts
                    Great stuff, thanks kp. And obviously thanks for the plug-in sharkbait.

                    I am using the Anti-Spam Email which outputs unicode (href="&#109;&#97;&#105;....), so I had to go off-piste a little:

                    $o = preg_replace('/&(?!amp;|#)/', "&",$o);	


                    It works. smiley

                      • 15175
                      • 116 Posts
                      Unfortunately this isn’t actually going to work unless I include every possible HTML entity that may be used (&copy;, &pound;, etc).
                      Just have to live with a few validation errors I guess.