Completed
Pull Request — master (#526)
by Michael
01:57
created

StringFunctions   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 43
ccs 18
cts 18
cp 1
rs 10
c 2
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mb_ucfirst() 0 8 1
A formatAsUsername() 0 7 1
A formatAsEmail() 0 6 1
1
<?php
2
3
class StringFunctions
4
{
5
	/**
6
	 * @param string $string
7
	 * @return string
8
	 */
9 1
	private static function mb_ucfirst($string)
10
	{
11 1
		$strlen = mb_strlen($string);
12 1
		$substr = mb_substr($string, 0, 1);
13 1
		$substr2 = mb_substr($string, 1, $strlen - 1);
14 1
		$upstring = mb_strtoupper($substr);
15 1
		$ustring = $upstring . $substr2;
16 1
		return $ustring;
17
	}
18
19
	/**
20
	 * Formats a string to be used as a username.
21
	 * @param $username
22
	 * @return string
23
	 */
24 1
	public static function formatAsUsername($username)
25
	{
26 1
		$uname = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $username);
27 1
		$uname = self::mb_ucfirst($uname);
28 1
		$uname = mb_ereg_replace("[ ]+", "_", $uname);
29 1
		$uname = mb_ereg_replace("[_]+$", "", $uname);
30 1
		return $uname;
31
	}
32
33
	/**
34
	 * Formats a string to be used as an email (specifically strips whitespace
35
	 * from the beginning/end of the Email, as well as immediately before/after
36
	 * the @ in the Email).
37
	 * @param $email
38
	 * @return string
39
	 */
40 1
	public static function formatAsEmail($email)
41
	{
42 1
		$newemail = mb_ereg_replace("^[ \t]+|[ \t]+$", "", $email);
43 1
		$newemail = mb_ereg_replace("[ \t]+@", "@", $newemail);
44 1
		$newemail = mb_ereg_replace("@[ \t]+", "@", $newemail);
45 1
		return $newemail;
46
	}
47
48
}
49