Detecting emails in a text

前端 未结 4 988
生来不讨喜
生来不讨喜 2020-12-31 05:52

I\'m trying to create a function that translates every occurrence of a plain text email address in a given string into it\'s htmlized version.

Let\'s say I h

4条回答
  •  温柔的废话
    2020-12-31 06:23

    $str = preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', '$1', $str);
    

    where ([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}) is the regular expression used for detecting an email address (this is a general example, email addresses may be more complicated than this and not all addresses may be covered, but finding the perfect regex for emails is up to you)

提交回复
热议问题