Email adress in text to mailto hyperlink (possible in Twig?)












0














I have an entity with a text-type attribute called contactInfo. Users can only submit text without html when entering these details in a form. Often an email address is entered somewhere in this textarea (together with more details).



Now when I display this contactInfo I would like adjust any email address to an email address with a mailto hyperlink. For example



Email us at: example@email.com. 


should become:



Email us at: <a href="mailto:example@email.com">example@email.com</a>.


How to go about this? Can I do this directly in Twig with some RegEx or Replace filter? Or should I really do this in the controller?










share|improve this question






















  • If I were you i'd adjust the input already before storing it in the database, otherwise u could do this by creating your own template class
    – DarkBee
    Nov 22 at 12:46
















0














I have an entity with a text-type attribute called contactInfo. Users can only submit text without html when entering these details in a form. Often an email address is entered somewhere in this textarea (together with more details).



Now when I display this contactInfo I would like adjust any email address to an email address with a mailto hyperlink. For example



Email us at: example@email.com. 


should become:



Email us at: <a href="mailto:example@email.com">example@email.com</a>.


How to go about this? Can I do this directly in Twig with some RegEx or Replace filter? Or should I really do this in the controller?










share|improve this question






















  • If I were you i'd adjust the input already before storing it in the database, otherwise u could do this by creating your own template class
    – DarkBee
    Nov 22 at 12:46














0












0








0







I have an entity with a text-type attribute called contactInfo. Users can only submit text without html when entering these details in a form. Often an email address is entered somewhere in this textarea (together with more details).



Now when I display this contactInfo I would like adjust any email address to an email address with a mailto hyperlink. For example



Email us at: example@email.com. 


should become:



Email us at: <a href="mailto:example@email.com">example@email.com</a>.


How to go about this? Can I do this directly in Twig with some RegEx or Replace filter? Or should I really do this in the controller?










share|improve this question













I have an entity with a text-type attribute called contactInfo. Users can only submit text without html when entering these details in a form. Often an email address is entered somewhere in this textarea (together with more details).



Now when I display this contactInfo I would like adjust any email address to an email address with a mailto hyperlink. For example



Email us at: example@email.com. 


should become:



Email us at: <a href="mailto:example@email.com">example@email.com</a>.


How to go about this? Can I do this directly in Twig with some RegEx or Replace filter? Or should I really do this in the controller?







php symfony twig






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 12:34









Dirk J. Faber

1,1421217




1,1421217












  • If I were you i'd adjust the input already before storing it in the database, otherwise u could do this by creating your own template class
    – DarkBee
    Nov 22 at 12:46


















  • If I were you i'd adjust the input already before storing it in the database, otherwise u could do this by creating your own template class
    – DarkBee
    Nov 22 at 12:46
















If I were you i'd adjust the input already before storing it in the database, otherwise u could do this by creating your own template class
– DarkBee
Nov 22 at 12:46




If I were you i'd adjust the input already before storing it in the database, otherwise u could do this by creating your own template class
– DarkBee
Nov 22 at 12:46












1 Answer
1






active

oldest

votes


















2














You can create a twig filter, something like "mailTo"
and do something like



<?php
namespace AppTwig;

use TwigExtensionAbstractExtension;
use TwigTwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters()
{
return array(
new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html')),
);
}

public function mailTo(string $text)
{
if(preg_match_all('/[p{L}0-9_.-]+@[0-9p{L}.-]+.[a-z.]{2,6}b/u',$text,$mails)){
foreach($mails[0]as $mail ){
$text = str_replace($mail,'<a href="mailto:'.$mail.'">'.$mail.'<a>',$text);
}
}


return $text;
}
}


And then use it like this in your template



contactInfo|mailTo


Either way, don't store "mailto:" or html tags in database when those are always gonna be the same...



documentation on twig custom filters
https://symfony.com/doc/current/templating/twig_extension.html






share|improve this answer























  • As OP stated Users can only submit text without html
    – DarkBee
    Nov 22 at 13:41










  • But they don't need to ... that's not how twig filter works, he can add dynamically html tags and render it with raw filter
    – Yoann Mir
    Nov 22 at 15:44










  • OP's enduser would not know about any variables so he/she would be forced to type something like bla bla bla mail me at {{ 'info@example.com' | mailTo }}. Not very convenient
    – DarkBee
    Nov 22 at 17:52








  • 2




    Well if OP strips all the html first before applying raw then yeah. You could also mark the filter as safe e.g. new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html'));
    – DarkBee
    Nov 22 at 20:22






  • 1




    @Dirk J. Faber if you have a better regexp, i can add it, this one is the first i found for the example but, like you say, it's not perfect
    – Yoann Mir
    Nov 23 at 8:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431163%2femail-adress-in-text-to-mailto-hyperlink-possible-in-twig%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can create a twig filter, something like "mailTo"
and do something like



<?php
namespace AppTwig;

use TwigExtensionAbstractExtension;
use TwigTwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters()
{
return array(
new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html')),
);
}

public function mailTo(string $text)
{
if(preg_match_all('/[p{L}0-9_.-]+@[0-9p{L}.-]+.[a-z.]{2,6}b/u',$text,$mails)){
foreach($mails[0]as $mail ){
$text = str_replace($mail,'<a href="mailto:'.$mail.'">'.$mail.'<a>',$text);
}
}


return $text;
}
}


And then use it like this in your template



contactInfo|mailTo


Either way, don't store "mailto:" or html tags in database when those are always gonna be the same...



documentation on twig custom filters
https://symfony.com/doc/current/templating/twig_extension.html






share|improve this answer























  • As OP stated Users can only submit text without html
    – DarkBee
    Nov 22 at 13:41










  • But they don't need to ... that's not how twig filter works, he can add dynamically html tags and render it with raw filter
    – Yoann Mir
    Nov 22 at 15:44










  • OP's enduser would not know about any variables so he/she would be forced to type something like bla bla bla mail me at {{ 'info@example.com' | mailTo }}. Not very convenient
    – DarkBee
    Nov 22 at 17:52








  • 2




    Well if OP strips all the html first before applying raw then yeah. You could also mark the filter as safe e.g. new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html'));
    – DarkBee
    Nov 22 at 20:22






  • 1




    @Dirk J. Faber if you have a better regexp, i can add it, this one is the first i found for the example but, like you say, it's not perfect
    – Yoann Mir
    Nov 23 at 8:25
















2














You can create a twig filter, something like "mailTo"
and do something like



<?php
namespace AppTwig;

use TwigExtensionAbstractExtension;
use TwigTwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters()
{
return array(
new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html')),
);
}

public function mailTo(string $text)
{
if(preg_match_all('/[p{L}0-9_.-]+@[0-9p{L}.-]+.[a-z.]{2,6}b/u',$text,$mails)){
foreach($mails[0]as $mail ){
$text = str_replace($mail,'<a href="mailto:'.$mail.'">'.$mail.'<a>',$text);
}
}


return $text;
}
}


And then use it like this in your template



contactInfo|mailTo


Either way, don't store "mailto:" or html tags in database when those are always gonna be the same...



documentation on twig custom filters
https://symfony.com/doc/current/templating/twig_extension.html






share|improve this answer























  • As OP stated Users can only submit text without html
    – DarkBee
    Nov 22 at 13:41










  • But they don't need to ... that's not how twig filter works, he can add dynamically html tags and render it with raw filter
    – Yoann Mir
    Nov 22 at 15:44










  • OP's enduser would not know about any variables so he/she would be forced to type something like bla bla bla mail me at {{ 'info@example.com' | mailTo }}. Not very convenient
    – DarkBee
    Nov 22 at 17:52








  • 2




    Well if OP strips all the html first before applying raw then yeah. You could also mark the filter as safe e.g. new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html'));
    – DarkBee
    Nov 22 at 20:22






  • 1




    @Dirk J. Faber if you have a better regexp, i can add it, this one is the first i found for the example but, like you say, it's not perfect
    – Yoann Mir
    Nov 23 at 8:25














2












2








2






You can create a twig filter, something like "mailTo"
and do something like



<?php
namespace AppTwig;

use TwigExtensionAbstractExtension;
use TwigTwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters()
{
return array(
new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html')),
);
}

public function mailTo(string $text)
{
if(preg_match_all('/[p{L}0-9_.-]+@[0-9p{L}.-]+.[a-z.]{2,6}b/u',$text,$mails)){
foreach($mails[0]as $mail ){
$text = str_replace($mail,'<a href="mailto:'.$mail.'">'.$mail.'<a>',$text);
}
}


return $text;
}
}


And then use it like this in your template



contactInfo|mailTo


Either way, don't store "mailto:" or html tags in database when those are always gonna be the same...



documentation on twig custom filters
https://symfony.com/doc/current/templating/twig_extension.html






share|improve this answer














You can create a twig filter, something like "mailTo"
and do something like



<?php
namespace AppTwig;

use TwigExtensionAbstractExtension;
use TwigTwigFilter;

class AppExtension extends AbstractExtension
{
public function getFilters()
{
return array(
new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html')),
);
}

public function mailTo(string $text)
{
if(preg_match_all('/[p{L}0-9_.-]+@[0-9p{L}.-]+.[a-z.]{2,6}b/u',$text,$mails)){
foreach($mails[0]as $mail ){
$text = str_replace($mail,'<a href="mailto:'.$mail.'">'.$mail.'<a>',$text);
}
}


return $text;
}
}


And then use it like this in your template



contactInfo|mailTo


Either way, don't store "mailto:" or html tags in database when those are always gonna be the same...



documentation on twig custom filters
https://symfony.com/doc/current/templating/twig_extension.html







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 at 8:48









DarkBee

8,60042741




8,60042741










answered Nov 22 at 13:34









Yoann Mir

865




865












  • As OP stated Users can only submit text without html
    – DarkBee
    Nov 22 at 13:41










  • But they don't need to ... that's not how twig filter works, he can add dynamically html tags and render it with raw filter
    – Yoann Mir
    Nov 22 at 15:44










  • OP's enduser would not know about any variables so he/she would be forced to type something like bla bla bla mail me at {{ 'info@example.com' | mailTo }}. Not very convenient
    – DarkBee
    Nov 22 at 17:52








  • 2




    Well if OP strips all the html first before applying raw then yeah. You could also mark the filter as safe e.g. new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html'));
    – DarkBee
    Nov 22 at 20:22






  • 1




    @Dirk J. Faber if you have a better regexp, i can add it, this one is the first i found for the example but, like you say, it's not perfect
    – Yoann Mir
    Nov 23 at 8:25


















  • As OP stated Users can only submit text without html
    – DarkBee
    Nov 22 at 13:41










  • But they don't need to ... that's not how twig filter works, he can add dynamically html tags and render it with raw filter
    – Yoann Mir
    Nov 22 at 15:44










  • OP's enduser would not know about any variables so he/she would be forced to type something like bla bla bla mail me at {{ 'info@example.com' | mailTo }}. Not very convenient
    – DarkBee
    Nov 22 at 17:52








  • 2




    Well if OP strips all the html first before applying raw then yeah. You could also mark the filter as safe e.g. new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html'));
    – DarkBee
    Nov 22 at 20:22






  • 1




    @Dirk J. Faber if you have a better regexp, i can add it, this one is the first i found for the example but, like you say, it's not perfect
    – Yoann Mir
    Nov 23 at 8:25
















As OP stated Users can only submit text without html
– DarkBee
Nov 22 at 13:41




As OP stated Users can only submit text without html
– DarkBee
Nov 22 at 13:41












But they don't need to ... that's not how twig filter works, he can add dynamically html tags and render it with raw filter
– Yoann Mir
Nov 22 at 15:44




But they don't need to ... that's not how twig filter works, he can add dynamically html tags and render it with raw filter
– Yoann Mir
Nov 22 at 15:44












OP's enduser would not know about any variables so he/she would be forced to type something like bla bla bla mail me at {{ 'info@example.com' | mailTo }}. Not very convenient
– DarkBee
Nov 22 at 17:52






OP's enduser would not know about any variables so he/she would be forced to type something like bla bla bla mail me at {{ 'info@example.com' | mailTo }}. Not very convenient
– DarkBee
Nov 22 at 17:52






2




2




Well if OP strips all the html first before applying raw then yeah. You could also mark the filter as safe e.g. new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html'));
– DarkBee
Nov 22 at 20:22




Well if OP strips all the html first before applying raw then yeah. You could also mark the filter as safe e.g. new TwigFilter('mailTo', array($this, 'mailTo'), array('is_safe' => 'html'));
– DarkBee
Nov 22 at 20:22




1




1




@Dirk J. Faber if you have a better regexp, i can add it, this one is the first i found for the example but, like you say, it's not perfect
– Yoann Mir
Nov 23 at 8:25




@Dirk J. Faber if you have a better regexp, i can add it, this one is the first i found for the example but, like you say, it's not perfect
– Yoann Mir
Nov 23 at 8:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431163%2femail-adress-in-text-to-mailto-hyperlink-possible-in-twig%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...