Format URL query

I’m trying to use the function to convert a string into a URL string–

Address: 5500 Grand Lake Drive, San Antonio, TX

5500 Grand Lake Drive, San Antonio, TX

Would expect the output to look like:
5500%20Grand%20Lake%20Drive%2C%20San%20Antonio%2C%20TX

Output comes out as:
5500 Grand Lake Drive, San Antonio, TX

Hi David, I haven’t played around much with the format tag yet, but based on some quick Googling it seems that the predefined characters that the htmlspecialchars() function are intended to format are:

& (ampersand) becomes &
" (double quote) becomes "
' (single quote) becomes '
< (less than) becomes &lt;
> (greater than) becomes &gt;

It seems that commas and whitespace characters aren’t covered in the htmlspecialchars() function so the output you mentioned seems like the expected output when using <Format code>.

It also seems that the <Format code> tag might only work when escaping text for inline <code> or <pre> block (based on the L&L documentation) since simply pasting <Format code>this & that</Format> into a template doesn’t seem to reformat the text output. The use case of this kind of tag is beyond me so a more experienced L&Ler might be able to confirm whether my understanding is correct, but for now, that seems like the most plausible explanation.

Hi David,

The newest plugin version has a feature that should work as you expected: the Format tag with attribute url_query will convert a given text to be suitable for URL query string.

<Format url_query>5500 Grand Lake Drive, San Antonio, TX</Format>

The above should output:

5500%20Grand%20Lake%20Drive%2C%20San%20Antonio%2C%20TX

As Ben pointed out, <Format code> uses htmlspecialchars, whereas the conversion you’re looking for is, I believe, rawurlencode.

I named the format type url_query, because it’s only suitable for a URL query string, not the whole URL, which can include characters like / and ?.

For example, the following:

<Format url_query>https://example.com/?query=...</Format>

…becomes…

https%3A%2F%2Fexample.com%2F%3Fquery%3D... 

So the main part of the URL needs to be outside of the Format tag.


I imagine you’d like to use the formatted URL query in a link. In that case, it might be easier to set the formatted value in a variable first, like:

<Set address><Format url_query>5500 Grand Lake Drive, San Antonio, TX</Format></Set>

…then pass it to the link href attribute:

<a href="https://example.com/?query={Get address}">Click here</a>

Awesome. Love it. Thanks for adding this in. When you’re calling a URL, or an API via URL string this is super valuable…