[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

text to html simple conversion

Hi,

I need a very basic text to html conversion function. All I need is to:

- replace special symbols with their alternatives (like < with &lt);

- put <br> in case of a single new line and put <p> in case of two new line chars in a row;

- put <a href> anchors if the text contains anything strting with http:// or https:// (so 'http://www.abc.com' would become <a href="http://www.abc.com">http://www.abc.com</a>
0
polygon
Asked:
polygon
1 Solution
 
ozoCommented:
s/(\n\n|[\n<>&])/${{'<'=>'&lt;','>'=>'&gt','&'=>'&amp;',"\n\n"=>'<p>',"\n"=>'<br>'}}{$1}/g;
s((\bhttps?://\S*\w))(<a href=$1>$1</a>)gi;
0
 
polygonAuthor Commented:
I just had to exchange the 2 statement as something was getting messy with <br> tags inside link anchors :)))

But it worked great for me, thanks a lot
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now