技术饭

PHP生成随机英文用户名

copylian    0 评论    15883 浏览    2020.02.25

最近用wordpress来做英文的博客,需要虚构一些随机的英文用户名来冒充访客发表评论,因为对PHP不熟悉(压根没学过),只能在百度和google查找方法。办法有2个:

1、随机字母数字组合法

这个方法的思路:26个字母大小写 +10个数字(0~9) 随机组合成6到10个的字符串最为用户名,代码如下:

function random_user($len = 8)

{

    $user = '';

    $lchar = 0;

    $char = 0;

    for($i = 0; $i < $len; $i++)

    {

        while($char == $lchar)

       {

            $char = rand(48, 109);

            if($char > 57) $char += 7;

            if($char > 90) $char += 6;

        }

       $user .= chr($char);

       $lchar = $char;

    }

return $user;

}

调用方法(默认生成8个字符串):

<?php echo random_user();?>

生产的英文用户名如下:

4c3yXssf,6IG0bdfa,u5NDh5d3,spBhdgPn,49otf8ub,NE8zg2dS

缺点很明显:不靠谱,可读性差,一看就知道是代码生成的。

2、字典随机组合法

方法思路:找出外国人最常用的姓(Most Common Surnames),最常用的名(Most Common Male Names,Most Common Female Names),再随机组合成一个常用的英文用户名。

function random_user()

{

      $male_names=array("James","John","Robert","Michael","William","David","Richard","Charles","Joseph","Thomas","Christopher","Daniel","Paul","Mark","Donald","George","Kenneth","Steven","Edward");

$famale_names = array("Mary","Patricia","Linda","Barbara","Elizabeth","Jennifer","Maria","Susan","Margaret","Dorothy","Lisa","Nancy","Karen","Betty","Helen","Sandra","Donna","Carol","Ruth");

$surnames =array("Smith","Jones","Taylor","Williams","Brown","Davies","Evans","Wilson","Thomas","Roberts","Johnson","Lewis","Walker","Robinson","Wood","Thompson","White","Watson","Jackson");

$frist_num = mt_rand(0,19);

$sur_num = mt_rand(0,19);

$type = rand(0,1);

if($type==0){

    $username=$male_names[$frist_num]." ".$surnames[$sur_num];

} else {

    $username=$famale_names[$frist_num]." ".$surnames[$sur_num];

}

      return $username;

}

这里使用了20个常用男性名,20常用个女性名,20个常用的姓来组合用户名,如果需要更多的数据可以看看这里:外国人最常用的100个姓,最常用的100个名。

调用的方法和上面是一样的,生成的英文用户名如下:

Mary Williams,Daniel Johnson,John Jones,Maria Evans,Donna Smith,Barbara Smith

可以看出这个方法生成的英文用户名很靠谱,可读性高,而且可生成数量众多(20*20+20*20 共800个)。最后我也是使用了这个函数。

外国人最常用的100个姓(100 Most Common Surnames):

1

Smith

51

Bailey

2

Jones

52

Parker

3

Taylor

53

Miller

4

Williams

54

Davis

5

Brown

55

Murphy

6

Davies

56

Price

7

Evans

57

Bell

8

Wilson

58

Baker

9

Thomas

59

Griffiths

10

Roberts

60

Kelly

11

Johnson

61

Simpson

12

Lewis

62

Marshall

13

Walker

63

Collins

14

Robinson

64

Bennett

15

Wood

65

Cox

16

Thompson

66

Richardson

17

White

67

Fox

18

Watson

68

Gray

19

Jackson

69

Rose

20

Wright

70

Chapman

21

Green

71

Hunt

22

Harris

72

Robertson

23

Cooper

73

Shaw

24

King

74

Reynolds

25

Lee

75

Lloyd

26

Martin

76

Ellis

27

Clarke

77

Richards

28

James

78

Russell

29

Morgan

79

Wilkinson

30

Hughes

80

Khan

31

Edwards

81

Graham

32

Hill

82

Stewart

33

Moore

83

Reid

34

Clark

84

Murray

35

Harrison

85

Powell

36

Scott

86

Palmer

37

Young

87

Holmes

38

Morris

88

Rogers

39

Hall

89

Stevens

40

Ward

90

Walsh

41

Turner

91

Hunter

42

Carter

92

Thomson

43

Phillips

93

Matthews

44

Mitchell

94

Ross

45

Patel

95

Owen

46

Adams

96

Mason

47

Campbell

97

Knight

48

Anderson

98

Kennedy

49

Allen

99

Butler

50

Cook

100

Saunders

美国人最常用的100个男性名(100 Most Common Male Names):

1

James

51

Joe

2

John

52

Juan

3

Robert

53

Jack

4

Michael

54

Albert

5

William

55

Jonathan

6

David

56

Justin

7

Richard

57

Terry

8

Charles

58

Gerald

9

Joseph

59

Keith

10

Thomas

60

Samuel

11

Christopher

61

Willie

12

Daniel

62

Ralph

13

Paul

63

Lawrence

14

Mark

64

Nicholas

15

Donald

65

Roy

16

George

66

Benjamin

17

Kenneth

67

Bruce

18

Steven

68

Brandon

19

Edward

69

Adam

20

Brian

70

Harry

21

Ronald

71

Fred

22

Anthony

72

Wayne

23

Kevin

73

Billy

24

Jason

74

Steve

25

Matthew

75

Louis

26

Gary

76

Jeremy

27

Timothy

77

Aaron

28

Jose

78

Randy

29

Larry

79

Howard

30

Jeffrey

80

Eugene

31

Frank

81

Carlos

32

Scott

82

Russell

33

Eric

83

Bobby

34

Stephen

84

Victor

35

Andrew

85

Martin

36

Raymond

86

Ernest

37

Gregory

87

Phillip

38

Joshua

88

Todd

39

Jerry

89

Jesse

40

Dennis

90

Craig

41

Walter

91

Alan

42

Patrick

92

Shawn

43

Peter

93

Clarence

44

Harold

94

Sean

45

Douglas

95

Philip

46

Henry

96

Chris

47

Carl

97

Johnny

48

Arthur

98

Earl

49

Ryan

99

Jimmy

50

Roger

100

Antonio

美国人最常用的100个女性名(100 Most Common Female Names):

1

Mary

51

Alice

2

Patricia

52

Julie

3

Linda

53

Heather

4

Barbara

54

Teresa

5

Elizabeth

55

Doris

6

Jennifer

56

Gloria

7

Maria

57

Evelyn

8

Susan

58

Jean

9

Margaret

59

Cheryl

10

Dorothy

60

Mildred

11

Lisa

61

Katherine

12

Nancy

62

Joan

13

Karen

63

Ashley

14

Betty

64

Judith

15

Helen

65

Rose

16

Sandra

66

Janice

17

Donna

67

Kelly

18

Carol

68

Nicole

19

Ruth

69

Judy

20

Sharon

70

Christina

21

Michelle

71

Kathy

22

Laura

72

Theresa

23

Sarah

73

Beverly

24

Kimberly

74

Denise

25

Deborah

75

Tammy

26

Jessica

76

Irene

27

Shirley

77

Jane

28

Cynthia

78

Lori

29

Angela

79

Rachel

30

Melissa

80

Marilyn

31

Brenda

81

Andrea

32

Amy

82

Kathryn

33

Anna

83

Louise

34

Rebecca

84

Sara

35

Virginia

85

Anne

36

Kathleen

86

Jacqueline

37

Pamela

87

Wanda

38

Martha

88

Bonnie

39

Debra

89

Julia

40

Amanda

90

Ruby

41

Stephanie

91

Lois

42

Carolyn

92

Tina

43

Christine

93

Phyllis

44

Marie

94

Norma

45

Janet

95

Paula

46

Catherine

96

Diana

47

Frances

97

Annie

48

Ann

98

Lillian

49

Joyce

99

Emily

50

Diane

100

Robin

只袄早~~~
感谢你的支持,我会继续努力!
扫码打赏,感谢您的支持!
php 英文 用户名 随机 

文明上网理性发言!

  • 还没有评论,沙发等你来抢