0

すべてのテキストが分析され、電子メールアドレスが#####$$$$.

よろしくお願いいたします。

4

1 に答える 1

1

You could find all email addresses in a text with a regex such as /\S*\@\S*/ (Test it on Rubular, might not be perfect) and then replace all matches with whatever you choose.

email_regex = /\S*\@\S*/
text = "This is test@example.com test string. Regex is regex@example.co.uk amazing."

result = text.gsub(email_regex, 'email_has_been_replaced')

p result
# => "This is email_has_been_replaced test string. Regex is email_has_been_replaced amazing."

In an ActiveRecord model:

class Post < AR::B
  EMAIL_REGEX = /\S*\@\S*/
  before_validation :remove_email_addresses_from_body

  private

  def remove_email_addresses_from_body
    self.body = body.gsub(EMAIL_REGEX, 'hidden_email')
  end
end
于 2013-10-30T18:50:07.433 に答える