Home Forums General Issues Escaping HTML – br tag


Escaping HTML – br tag

  • I have a sub field I’m trying to translate to a string so it can return a url that Google Maps understands.

    <?php $address = get_sub_field('address');
    <a href="//<?php echo $address; ?>">Get directions</a>

    The problem is my ACF field is a textarea with <br />, so my string looks like this: Someplace Dr<br />Suite D<br />New Haven, CT <br />06501

    Google Maps will “translate” spaces and commas as is, but not <br />.

    I tried this, which does not work: <a href="//<?php echo esc_html($address); ?>">Get directions</a>

    Any ideas?

  • If you don’t need the <br> tags to display the address then change the format of the textarea to “No Formatting”. If you do need the breaks then you’ll need to do a string replace to remove them. Something like

    $address = str_replace('<br />', ', ', $address);

    I’d probably also make it a proper query value with

    $address = urlencode($address);
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Escaping HTML – br tag’ is closed to new replies.