Jak zrobic funkcje maskujaca email ?

0

Mam taka funkcje w php która maskuje adres email w zależności od długości identyfikatora żytkownika.

  1. rozdziela on adres email względem znaku @
  2. pobiera długość identyfikatora użytkownika
  3. pobieram pierwsza i ostatnią litere tego identyfikatora
  4. sklejam maskowany email (pierwsza litera + ***** (tyle ile dlugosc identyfikatora - 2) + ostatni znak
  5. Wiec adres: [email protected] = a*******[email protected]
  6. Adresy krotsze lub rowne 4 znaki wygladaja: [email protected] = **@waclaw.pl
        function hiddenMail($mail) {
        	$arrayStr = explode('@', $mail); 	
        	$lenStr = strlen($arrayStr[0]);
        	if ($lenStr >= 4) {
        		$first = substr($arrayStr[0], 0, 1);
        		$last = substr($arrayStr[0], -1);
        		$lenStr = $lenStr - 2;
        		$hiddenEmail = $first;
        		for ($i = 0; $i < $lenStr; $i++) {
        			$hiddenEmail .= '*';
        		}
        		$hiddenEmail .= $last;
        		$hiddenEmail .= '@'.$arrayStr[1];
        	} else {
        		$hiddenEmail = '**@'.$arrayStr[1];
        	}
        	
        	return $hiddenEmail;
        }

Chciałbym zrobic taka funkcje na bazie postgresql zeby juz w zapytaniu otrzymać taki zamaskowany email. Czy ma ktoś może podobną funkcję lub wie gdzi emożna poszukać gotowego rozwiązania. Albo jak w psql rozdzielić pierwszą część maila to może coś wymyśle.

1

masz gotowca:

CREATE OR REPLACE FUNCTION hidden_mail(mail text)
  RETURNS text AS
$BODY$
declare 
	adres text=''::text;
	domena text=''::TEXT;
begin
	adres=substring(mail, 1,position('@' IN mail)-1);
	domena=substring(mail, position('@' IN mail)+1, length(mail));
	if adres is null or domena is null then 
		return null;
	end if;
	if length(adres)>4 then 
		adres=substring(adres,1,1) || lpad('', length(adres)-2, '*') || substring(adres, length(adres), 1);
	else
		/* adres=lpad('', length(adres), '*'); */
		adres='**';
	end if;
	return adres || '@' || domena;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
  COST 1;

select hidden_mail(mail) from unnest(array['[email protected]', '[email protected]', '[email protected]', '[email protected]']) as x(mail)
0

Użyj funkcji overlay na stronie podanej przez abrakadaber jest to ładnie opisane

0

Dzieki zapomoc :) wlzlem na te strone co podaliscie wyzej i napisalem taka funkcje

CREATE OR REPLACE FUNCTION foo(IN email text) RETURNS text AS
        $$
            DECLARE
                len_id_email integer;
                id_email text;
                id_domain text;
                first text;
                last text;
                hidden_email text;
            BEGIN
                id_email  = split_part(email, '@', 1);
                id_domain = split_part(email, '@', 2);
                len_id_email = char_length(id_email);
    
                IF len_id_email > 4 THEN
                    first = substr(email, 1, 1);
                    last = substr(email, len_id_email, 1);
                    len_id_email = len_id_email -2;
                    hidden_email = first;
       
                    FOR i IN 1 .. len_id_email LOOP
                        hidden_email = hidden_email || '*';
                    END LOOP;
        
                    hidden_email = hidden_email || last;
                    hidden_email = hidden_email|| '@' || id_domain;
         
                ELSE
                    hidden_email = '**@' || id_domain;
                END IF; 
                
                RETURN hidden_email;
            END;
        $$
        LANGUAGE 'plpgsql';

1 użytkowników online, w tym zalogowanych: 0, gości: 1