Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php random number generation

Posted on 2010-11-08
5
Medium Priority
?
242 Views
Last Modified: 2012-05-10
I am designing a booking contact form with PHP and part of the response the client gets on form submission will be a unique 10 character letter string (to identify their booking in the system). Is there code to allow me to generate this unique letter string?
Thanks,
N
0
Comment
Question by:Neil_Bradley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
rogerard earned 1500 total points
ID: 34087685
Here you go, from this website: http://www.lost-in-code.com/programming/php-code/php-random-string-with-numbers-and-letters/

function genRandomString() {
    $length = 10;
    $characters = ’0123456789abcdefghijklmnopqrstuvwxyz’;
    $string = ”;    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}

Open in new window

0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 34087767
Looks like what I am after however I am getting a syntax error warning on this line
$string .= $characters[mt_rand(0, strlen($characters))];
0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 34087849
I modified it a little and it worked great.

function genRandomString() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; 

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}
echo genRandomString();

Open in new window

0
 
LVL 7

Expert Comment

by:rogerard
ID: 34087851
If you copied my code exactly, then you'll need to replace all of the ' with ".  Apparently, what I copied and pasted, php doesn't like.
0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 34087854
Thanks for that. A slightly modified version worked perfectly.
N
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question