SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot Siick26's Avatar
    Join Date
    May 2013
    Location
    England
    Posts
    113
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to generate words instead of characters?

    I'm using a script which generates words, but instead of generating words, it generates characters instead and mixes them up.

    This is the code

    PHP Code:
    <?php 
    function FirstNames($numchar

        
    $word "Jason, Abdul, Aria, Demi, Paul"
        
    $array=explode(",",$word); 
        
    shuffle($array); 
        
    $newstring implode($array,""); 
        return 
    substr($newstring0$numchar); 



    ?>
    And this is the code which i use to echo the results, it's like a name generator basically.

    PHP Code:
    <?php

    $num_letters 
    rand(520);
    echo 
    FirstNames($num_letters);
    ?>
    Someone gave me the script, hence i didn't write it myself, but the use was originally intended to generate a random string such as "abs783" not actual words.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function RandFirstName () {
      
    $words = array( 'Jason''Abdul''Aria''Demi''Paul' ); // Fill this in.
      
    return $wordsarray_rand$words ) ];
    }

    echo 
    RandFirstName(); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Zealot Siick26's Avatar
    Join Date
    May 2013
    Location
    England
    Posts
    113
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, so do i replace the top half of the code with the one you posted? and keep the bottom half the same? (ie the part where i want the string to display)?

    Edit: I see now, i've done it. Works just fine. Thanks for that. Say i wanted to generate two words, how would i go about that?
    Say i wanted to generate "Abdul and Peter"

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Don't keep the bottom half, and can change most if not all of the code in the top half to what I provided.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Zealot Siick26's Avatar
    Join Date
    May 2013
    Location
    England
    Posts
    113
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Don't keep the bottom half, and can change most if not all of the code in the top half to what I provided.
    I see now, i've done it. Works just fine. Thanks for that. Say i wanted to generate two words, how would i go about that?
    Say i wanted to generate "Abdul and Peter"

    P.S i'm working on a name generator, and i will be sure to credit you on my site for your help.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Well the easiest way would be to run the function twice...but there is that chance of repeat names. But if you have a large data set of names in, it would be very rare.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •