Topic: Encoding ampersands on the fly for site validation  (Read 501 times)

Pages: [1]   Go Down

#1: 29-Jun-2009, 08:10 AM

aladage
Posts: 84

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!

#2: 29-Jun-2009, 08:40 AM


sharkbait
Posts: 1,483

i couldn't find it in the repository...

i use this:
Code:
//<?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 &amp;
$o preg_replace('%&(?![a-zA-Z0-9#]{1,6})%i'"&amp;",$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

#3: 29-Jun-2009, 08:59 AM

aladage
Posts: 84

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

#4: 29-Jun-2009, 09:01 AM

aladage
Posts: 84

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!

#5: 28-Nov-2009, 06:57 AM

j_p
Posts: 107

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.

« Last Edit: 28-Nov-2009, 07:03 AM by j_p »

#6: 28-Nov-2009, 07:00 AM


sharkbait
Posts: 1,483

Quote
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  ?>

#7: 28-Nov-2009, 07:07 AM

j_p
Posts: 107

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.

#8: 28-Nov-2009, 11:24 AM


kp52
Posts: 415

WWW
The last part of the code for the API makeURL function may be of interest if you're diving into regular expressions:
Code:
        if ($this->config['xhtml_urls']) {return preg_replace("/&(?!amp;)/","&amp;", $host . $virtualDir . $url);}
Cheesy KP

#9: 28-Nov-2009, 01:07 PM

j_p
Posts: 107

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:

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

It works. Smiley

#10: 28-Nov-2009, 01:43 PM

j_p
Posts: 107

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.

#11: 29-Nov-2009, 04:58 AM

Coding Team

sottwell
Posts: 10,439

WWW
Wouldn't a non-validating & usually have a space on either side of it? I would think a simple
Code:
str_replace(' & ', ' &amp; ', $tring)
would solve 99+% of the validation issues.

Either breaking out all javascript to external files or enclosing it in CDATA tags would also help.
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#12: 29-Nov-2009, 06:04 AM

j_p
Posts: 107


Sharkbait's code works for this situation where there is space on either side of the ampersand as well.

I wanted to come up with a solution that would work if the ampersand had characters of either side of it, as what happens in some external link URL's.

However, I've just updated all the necessary href URL's manually within the templates now - I was just looking for a quick fix.


#13: 29-Nov-2009, 01:55 PM

Coding Team

sottwell
Posts: 10,439

WWW
Perhaps this would be useful? http://php.net/manual/en/function.htmlspecialchars.php
sottwell.com has moved to a lovely Solaris 10 server!
Log in username guest, password guestuser.
Templates are now becoming available at http://sottwell.com/templates.html

#14: 29-Nov-2009, 02:41 PM

j_p
Posts: 107

Thanks for the link Susan - I'll have a closer look at it later.
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.