Skip to content

Commit

Permalink
Add method to extract emails from string.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Jan 17, 2022
1 parent e33c696 commit 1d281a3
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/Helpers/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
class Text
{

/**
* Contains a cache map of previously humanized words.
*
Expand All @@ -19,7 +18,7 @@ public static function acronym($string)
{
$string = trim($string);

if(!strlen($string)){
if (!strlen($string)) {
return $string;
}

Expand Down Expand Up @@ -60,7 +59,7 @@ public static function endsWith($needle, $haystack)

public static function properize($string)
{
return $string.'\''.($string[strlen($string) - 1] != 's' ? 's' : '');
return $string . '\'' . ($string[strlen($string) - 1] != 's' ? 's' : '');
}

public static function generateString($length = 8)
Expand Down Expand Up @@ -103,7 +102,7 @@ public static function shorten($string, $wordsreturned)
/* Need to chop of some words
*/
array_splice($array, $wordsreturned);
$retval = implode(" ", $array)." ...";
$retval = implode(" ", $array) . " ...";
}
return $retval;
}
Expand Down Expand Up @@ -262,7 +261,7 @@ function alphaID($in, $to_num = false, $pad_up = 3, $passKey = null)
for ($t = floor(log($in, $base)); $t >= 0; $t--) {
$bcp = bcpow($base, $t);
$a = floor($in / $bcp) % $base;
$out = $out.substr($index, $a, 1);
$out = $out . substr($index, $a, 1);
$in = $in - ($a * $bcp);
}
$out = strrev($out); // reverse
Expand Down Expand Up @@ -323,14 +322,14 @@ function extractCommonWords($string)

foreach ($matchWords as $key => $item) {
if ($item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3) {
unset ($matchWords[$key]);
unset($matchWords[$key]);
}
}
$wordCountArr = [];
if (is_array($matchWords)) {
foreach ($matchWords as $key => $val) {
$val = strtolower($val);
if (isset ($wordCountArr[$val])) {
if (isset($wordCountArr[$val])) {
$wordCountArr[$val]++;
} else {
$wordCountArr[$val] = 1;
Expand Down Expand Up @@ -537,7 +536,7 @@ public static function autoSequence($text, $n = 1, $startFrom = null)
*/
public static function humanize($word, $separator = '_')
{
if (isset(static::$_humanized[$key = $word.':'.$separator])) {
if (isset(static::$_humanized[$key = $word . ':' . $separator])) {
return static::$_humanized[$key];
}
return static::$_humanized[$key] = ucwords(str_replace($separator, " ", $word));
Expand Down Expand Up @@ -584,7 +583,7 @@ private static function autolink_do($text, $sub, $limit, $tagfill, $auto_title,
$pre = substr($text, 0, $pos);
$post = substr($text, $pos + $sub_len);

$fail_text = $pre_hit.$hit;
$fail_text = $pre_hit . $hit;
$fail_len = strlen($fail_text);

#
Expand Down Expand Up @@ -629,7 +628,7 @@ private static function autolink_do($text, $sub, $limit, $tagfill, $auto_title,
if ($ok) {
if (preg_match('/^([a-z0-9\-\.\/\-_%~!?=,:;&+*#@\(\)\$]+)/i', $post, $matches)) {

$url = $hit.$matches[1];
$url = $hit . $matches[1];

$cursor += strlen($url) + strlen($pre_hit);
$buffer .= $pre_hit;
Expand Down Expand Up @@ -661,7 +660,7 @@ private static function autolink_do($text, $sub, $limit, $tagfill, $auto_title,
$display_url = $url;

if ($force_prefix) {
$link_url = $force_prefix.$link_url;
$link_url = $force_prefix . $link_url;
}

if ($GLOBALS['autolink_options']['strip_protocols']) {
Expand All @@ -683,7 +682,7 @@ private static function autolink_do($text, $sub, $limit, $tagfill, $auto_title,

if (!preg_match("!^(http|https)://{$display_quoted}$!i", $link_url)) {

$tagfill .= ' title="'.$link_url.'"';
$tagfill .= ' title="' . $link_url . '"';
}
}

Expand Down Expand Up @@ -718,7 +717,7 @@ private static function autolink_label($text, $limit)
}

if (strlen($text) > $limit) {
return substr($text, 0, $limit - 3).'...';
return substr($text, 0, $limit - 3) . '...';
}

return $text;
Expand Down Expand Up @@ -755,7 +754,7 @@ private static function autolink_email($text, $tagfill = '')
$hit = substr($text, $pos, 1);
$post = substr($text, $pos + 1);

$fail_text = $pre.$hit;
$fail_text = $pre . $hit;
$fail_len = strlen($fail_text);

#die("$pre::$hit::$post::$fail_text");
Expand Down Expand Up @@ -788,7 +787,7 @@ private static function autolink_email($text, $tagfill = '')
$len = strlen($matches[1]);
$plen = strlen($pre);

$hit = substr($pre, $plen - $len).$hit;
$hit = substr($pre, $plen - $len) . $hit;
$pre = substr($pre, 0, $plen - $len);
} else {

Expand Down Expand Up @@ -842,4 +841,16 @@ private static function autolink_email($text, $tagfill = '')

return $buffer;
}
}

public static function extractEmails($string)
{
foreach (preg_split('/\s/', $string) as $token) {
$email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
if ($email !== false) {
$emails[] = $email;
}
}

return $emails;
}
}

0 comments on commit 1d281a3

Please sign in to comment.