Forum code that automatically turns urls into clickable links

16 posts by 2 authors in: Forums > CMS Builder
Last Post: December 29, 2018   (RSS)

By gkornbluth - December 28, 2018

Oops, sorry, I didn't catch the typo. My bad.

The test record ‘recipe’ text box contains only the following as text:

______________________________________

TEXT BEFORE A LINK

http://www.thecmsbcookbook.com

AFTER LINK

qzl
AFTER A Q-Z-L
<a href="http://www.thecmsbcookbook.com/css/fonts2019.css.php" />CLICK TO ACCEPT THIS</a>
lzq
AFTER AN L-Z-Q

_______________________________Z_____

As always, you're the best!

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By daniel - December 28, 2018

Hey Jerry,

I found the other issue - the two htmlPurify() calls are missing the 2nd parameter, so it simply wasn't running. It should be:

  // did we find an ending blockquote?
  if (count( $tmp ) > 1) {

    // purify content after blockquote
    $tmp[1] = htmlPurify($tmp[1], $config);
  }
  else {

    // purify only element
    $tmp[0] = htmlPurify($tmp[0], $config);
  }

As for the link rendering within the blockquote, that is because the content already contains an <a></a> tag which is being rendered into a link. If you also want the link to not render, that's a separate issue from the purifier.

Let me know any questions!

Daniel
Technical Lead
interactivetools.com

By daniel - December 28, 2018 - edited: December 28, 2018

Hi Jerry,

$config was from the very first snippet I posted and needs to be initialized before using htmlPurify:

$config = array();
$config['AutoFormat.Linkify'] = true;

I do understand what you're asking for; though there are a few differences between your use case and what happens here on the forum. If you paste HTML into the forum, such as "<a href='thecmsbcookbook.com/test.php">Not A Live Link When In Block Quotes</a>" the forum encodes the HTML so that it doesn't render, and instead displays the HTML tags as text. If your $recipe variable contains unencoded HTML it will render as HTML when output - regardless of anything the purifier has done.

(Also, worth noting that links are added within blockquotes on these forums if you include the full URL):

<a href='http://thecmsbcookbook.com/test.php">Live Link When In Block Quotes</a>

So, if you want to be able to display HTML as text in your recipes, you'll need to save them as encoded HTML. Since anything copied from the forums should already be encoded, I think you're mostly covered, but you can also manually encode/decode online with a tool such as https://www.online-toolz.com/tools/text-html-entities-convertor.php

Hopefully that does the trick!

Thanks,

Daniel
Technical Lead
interactivetools.com

By gkornbluth - December 28, 2018

Hi Daniel,

Appreciate your keeping up with this.

I mistakenly left the

$config = array();
$config['AutoFormat.Linkify'] = true;

out when I updated the code.

Maybe some of the issues I'm still having are because I've over simplified the test code.

Let me put all the original recipe code back in to the page and see if that solves any of the issues in the Block Quotes.

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - December 29, 2018

Hi Daniel,

When I put  the rest of the code into the viewers, everything worked as planned.

Couldn't have done this without your help. Many thanks!

I've created 2 recipes in my CMSB Cookbook to document the process.

A simple implementation of the htmlPurify library

http://www.thecmsbcookbook.com/recipedetail.php?590

And a more complex implementation for the Cookbook

http://www.thecmsbcookbook.com/recipedetail.php?324

Hope they're helpful to others.

Best for the new year!

Jerry Kornluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php