ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Ampersands not encoded in URLs?

August 08, 2008 11:27am

Subscribe [4]
  • #1 / Aug 08, 2008 11:27am

    44sunsets

    42 posts

    Isn’t there a more elegant solution to this? I was very surprised that “Automatically Convert High ASCII Text to Entities” setting wasn’t encoding ampersands automatically—that means you won’t get valid HTML, which is a big no-no.

    In the end, I ended up using the XML plugin to encode the ampersands automatically. It’s not an ideal solution (since I don’t like the hex entities) but it’ll have to do.

    And Derek, that find and replace solution isn’t safe to use because sometimes the ampersand is already encoded, so it would get encoded again which isn’t what we want.

    I’m surprised not more people have mentioned this problem.

    I’m also really surprised that the Smarty Pants plugin doesn’t encode ampersands either. It seems like ampersand really is the odd duck out.

    [Mod edit: Split from this thread]

  • #2 / Aug 08, 2008 11:37am

    Derek Jones

    7561 posts

    44sunsets, this post is from 2006.  What version are you running?  EE has coded encoded ampersands in entry titles for many many versions.

  • #3 / Aug 08, 2008 11:43am

    44sunsets

    42 posts

    I’m running 1.6.1

    Sorry, I just re-read the original post and the OP was talking about titles. I’m actually using a custom field for URLs. So the field would contains URLs such as:
    http://maps.google.com/maps?f=q&hl=en&geocode;=&q=San+Diego+Convention+Center&sll=32.708408,-117.162377&sspn=0.0065,0.013947&ie=UTF8&ll=32.707767,-117.162377&spn=0.0065,0.013947&z=17

    These have lots of messy ampersands that need to be manually encoded, but I can’t use XHTML plugin on that since it puts tags around it which would make my HTML invalid.

    See here for my real-life example (look at the “View map” URL):
    http://www.queeniechan.com/events/archive/san_diego_comic_con_international_2008/

    I’m using the XML plugin to encode this custom field at the moment, but I’d prefer something more elegant since the XML encodes stuff like hyphens in the URL as well, which I’d rather leave untouched.

    I guess my situation is different to the OP’s, so maybe I would’ve been better off posting in my own thread.

  • #4 / Aug 08, 2008 11:49am

    Derek Jones

    7561 posts

    No worries, I’ve split you to a new support thread.  So what formatting type do you have selected for that custom field on that published entry?

  • #5 / Aug 08, 2008 12:00pm

    44sunsets

    42 posts

    Oops, thanks for that Derek—though I wasn’t really intending for this to become an actual support thread as my problem isn’t that serious. It was more venting than anything else 😊

    I’m now using the “XML Encode” option in the “Default Text Formatting for This Field”. Initially I was using the XML plugin in the actual template code where I was outputting the blah blah, but I figured it’s more elegant if the text formatting was done in the background.

    I might try some of the other plugins in the “Default Text Formatting for This Field” to see if they can do it better.

  • #6 / Aug 08, 2008 12:04pm

    Derek Jones

    7561 posts

    For some background: ExpressionEngine won’t mess with the content you input unless there is formatting applied.  Obviously, this is highly desirable as it allows complete control of your content, either with specific plugins for formatting, or using “None” to leave it entirely under your control.  EE’s Auto XHTML typography is what is the appropriate choice for letting content authors publish and turning their unformatted content into valid markup.  I’m not fully understanding why this formatting option isn’t appropriate for you, can you elaborate?  It would do what you’re expecting EE to do to your content.

  • #7 / Aug 08, 2008 12:15pm

    44sunsets

    42 posts

    [edit: reposted below]

  • #8 / Aug 08, 2008 12:19pm

    Derek Jones

    7561 posts

    The forums’ security filters are stripping some of your code sample as it’s not able to validate the tag.  Can you repost, and make sure that if you use double quotes around the href, that all interior quotes are single, or vice versa.

    As far as what you’re describing in general, a paragraph tag around a link is not invalid markup.  Hopefully your reposted code sample will provide some clarity.

  • #9 / Aug 08, 2008 12:29pm

    44sunsets

    42 posts

    EE’s Auto XHTML typography is what is the appropriate choice for letting content authors publish and turning their unformatted content into valid markup.  I’m not fully understanding why this formatting option isn’t appropriate for you, can you elaborate?  It would do what you’re expecting EE to do to your content.

    I assume by “Auto XHTML” you mean the “Xhtml” option in “Default Text Formatting for This Field”?
    I tried that, but the problem is that it wraps my URL with tags so my output ends up being invalid, e.g.:

    <a href="http://p">http://www.example.com[/p]"]View map</a>

    (sorry, I can’t seem to use the proper tags in the above code so I used [p] and substituted square brackets instead of angle brackets)

    I know it’s a bit confusing so I’ll try to explain it better: the <a> tag and link text I’m outputting is hardcoded in the template, so it’s ony the href attribute which outputs its value from my custom field. Basically my EE template code says:

    <a href="http://{event_map_url}">View map</a>

    That’s why having the tag around the custom field is invalid HTML.

  • #10 / Aug 08, 2008 12:32pm

    Derek Jones

    7561 posts

    Ok, I understand now, I thought you were putting the link markup in your entry.  Sounds like a good reason for a new plugin, I’ll put our Code Mechanic on it, as it will be useful for more than just yourself.

  • #11 / Aug 13, 2008 8:10pm

    44sunsets

    42 posts

    Thanks a lot for your help, Derek!

  • #12 / Aug 13, 2008 8:26pm

    Mark Bowen

    12637 posts

    Hiya,

    It’s late here and so hopefully I have understood this correctly and perhaps the Find and Replace plugin may help in this situation?

    {exp:replace find="&" replace="&"}
    {event_map_url}
    {/exp:replace}

    Hope I was reading this correctly and the plugin is of use.

    Best wishes,

    Mark

  • #13 / Aug 13, 2008 8:29pm

    Derek Jones

    7561 posts

    That’s in the original thread Mark, but thank you.

  • #14 / Aug 13, 2008 8:30pm

    44sunsets

    42 posts

    Thanks Mark, but the reason I can’t use the find and replace is because in some cases, the URL may already have its ampersands encoded entities, so doing the replace would end up mangling things.

  • #15 / Aug 13, 2008 8:33pm

    Mark Bowen

    12637 posts

    That’s in the original thread Mark, but thank you.

    Told you it was late here 😉

    Thanks Mark, but the reason I can’t use the find and replace is because in some cases, the URL may already have its ampersands encoded entities, so doing the replace would end up mangling things.

    Ah I see now. Yep a plugin would be able to be written I suppose to check if the ampersands already are encoded and if they are then don’t touch them and if they aren’t then convert them.

    I’m sure Justin will get something up and running for you in no time. If not (highly doubtful 😉 ) I wouldn’t mind having a go at this myself when I get a free moment. In fact I might just do anyway just to see if I can do it although I’m 150% positive Justin will beat me to it!! 😊

    Best wishes,

    Mark

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases