Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
1.1k views
in Q2A Core by
edited by

As the question subject states, I would like to know what is the best way to filter email addresses from posts (questions/answers/comments)?

HTMLawed has no inbuilt function for this, however, the developer suggested a regex that I can call on the content: http://www.bioinformatics.org/phplabware/forum/viewtopic.php?id=249

// Text is run through htmLawed
$text = htmLawed($text, ...);

// replace email address not in an 'href' attribute value
$emailAddress_pattern = '`(^|[^:])\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b`i';
$emailAddress_replacement = '***'; // can be empty

if(strpos('@', $text)){ // avoiding preg_replace if no emailAddress_pattern
 $text = preg_replace($emailAddress_pattern, $emailAddress_replacement, $text);
}



But: Where do I have to implement this code / in core? In qa_sanitize_html() ?

But is qa_sanitize_html() called on every question/answer/comment output?

Thanks,
Kai

1 Answer

0 votes
by
Create a plugin with a filter module, and filter the content of questions, answers and comments.
by
Thanks, gid. I thought you would mention to create a plugin :)

I actually would like to know where I could put the code in the *core*. This tiny hack, in my point of view, is not worth a plugin...
by
You can put it inside the filter module in qa-filter-basic.php - it works exactly the same way as a filter module in a plugin.
by
mhhh seems complicated to add the code to all 3 filter functions Q/A/C.
Alright, I decided to use jquery and filter mails frontend :)

Thanks anyways, it might help others.
by
Now that I understood the filter plugin I wrote an alpha version that filters out email addresses and sets asterisks ***

In function filter_question():

// filter and mask email addresses
$pattern = '/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/';
$replacement = '******';
$content = preg_replace($pattern, $replacement, $content);

Done!
...