]> 91.132.146.200 Git - dolphin.git/commitdiff
extract emails from string
authorBanana <banana@optimus.de>
Tue, 16 Jul 2019 13:35:04 +0000 (15:35 +0200)
committerBanana <banana@optimus.de>
Tue, 16 Jul 2019 13:35:04 +0000 (15:35 +0200)
single-functions/extract-email-links.php [new file with mode: 0644]

diff --git a/single-functions/extract-email-links.php b/single-functions/extract-email-links.php
new file mode 100644 (file)
index 0000000..feb7a34
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+/**
+ *  dolphin. Collection of useful PHP skeletons.
+ *  Copyright (C) 2019  Johannes 'Banana' Keßler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+/**
+ * extract from given string (was email body) any links we want to add
+ * should be in the right format
+ * return an array with links and the infos about them
+ *
+ * @param string $string
+ * @return array $ret
+ */
+static function extractEmailLinks($string) {
+    $ret = array();
+
+    #this matches a valid URL. An URL with | is still valid...
+    $urlpattern  = '#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#';
+
+    preg_match_all($urlpattern, $string, $matches);
+    if(isset($matches[0]) && !empty($matches[0])) {
+        foreach($matches[0] as $match) {
+            $ret[md5($match)] = $match;
+        }
+    }
+
+
+    return $ret;
+}