Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Quick PHP Help

  1. #1
    Join Date
    Aug 2005
    Location
    Standing on the rooftops...
    Posts
    1,501
    Tokens
    6
    Habbo
    ReviewDude

    Latest Awards:

    Default Quick PHP Help

    Hey,

    I've tried everything I know (which, admittedly, is limited), and I would appreciate your help, oh great coding community of HabboxForum.

    Basically, I want to ensure that the information I get from a form is just a single word, with no spaces or other characters, how would I go about this?

    Also, if possible, is there any way I can automatically capitalise the first letter of a word in a variable?

    Thanks,

    Patrick
    The sunlight hurts my eyes...

    ~ Love, Patrick ~


    Know your stuff about Habbo? I'm looking for high-quality article writers - PM for more!

    I am Habbox's most trusted seller of VIP/Donator - over 100 months total sold without issue.

  2. #2
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    PHP Code:
    <?php
    /* Get first word */
    $String 'hello my name is james and i love you';

    $First_Word currentexplode' '$String ) );

    echo 
    $First_Word;
    Echos
    hello

    PHP Code:
    <?php
    /* Capitalize each word */
    $String 'hello my name is james and i love you';

    echo 
    ucwords$String );
    Echos
    Hello My Name Is James And I Love You
    Hi, names James. I am a web developer.

  3. #3
    Join Date
    Aug 2005
    Location
    Standing on the rooftops...
    Posts
    1,501
    Tokens
    6
    Habbo
    ReviewDude

    Latest Awards:

    Default

    Quote Originally Posted by Protege View Post
    PHP Code:
    <?php
    /* Get first word */
    $String 'hello my name is james and i love you';

    $First_Word currentexplode' '$String ) );

    echo 
    $First_Word;
    Echos
    hello

    PHP Code:
    <?php
    /* Capitalize each word */
    $String 'hello my name is james and i love you';

    echo 
    ucwords$String );
    Echos
    Hello My Name Is James And I Love You
    That works splendidly, thank you, +rep

    All I need now is to limit it to a single word.
    The sunlight hurts my eyes...

    ~ Love, Patrick ~


    Know your stuff about Habbo? I'm looking for high-quality article writers - PM for more!

    I am Habbox's most trusted seller of VIP/Donator - over 100 months total sold without issue.

  4. #4
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    You mean client side?

    This then

    HTML Code:
    <script type="text/javascript">
    function CheckSpaces( obj )
    {
        if( obj.value.match( /\s/g ) )
        {
            obj.value = obj.value.replace( /\s/g,'' );
        }
    }
    </script>
    
    <input type="text" onkeyup="CheckSpaces( this )">
    Aint tested it though, however it should work.
    Hi, names James. I am a web developer.

  5. #5

    Default

    I wouldn't rely on client-side validation as they could have JavaScript off, as for what you want to do you can easily do this.
    PHP Code:
    $string trim($string);

    if (
    preg_match('#^(\w+)$#'$string$matches) === 0)
    {
      
    // no match
    }

    else
    {
      
    // match

    You need the trim in there because the regexp won't match " word" etc. Also it will ensure the string only have one word so you're killing two birds with one stone.
    Last edited by Iszak; 30-04-2009 at 07:27 PM. Reason: Changed code to php tags

  6. #6
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default

    Yet my server-side version also did the trick.
    Hi, names James. I am a web developer.

  7. #7

    Default

    But like I said - do not rely on JavaScript validation, because they could just as easily have it turnt off. If you still want to use JavaScript validation, you should still have server side validation too.

  8. #8
    Join Date
    Nov 2007
    Posts
    1,253
    Tokens
    150

    Latest Awards:

    Default

    but he did... that was his first reply.

  9. #9
    Join Date
    Aug 2005
    Location
    Standing on the rooftops...
    Posts
    1,501
    Tokens
    6
    Habbo
    ReviewDude

    Latest Awards:

    Default

    Quote Originally Posted by Protege View Post
    You mean client side?

    This then

    HTML Code:
    <script type="text/javascript">
    function CheckSpaces( obj )
    {
        if( obj.value.match( /\s/g ) )
        {
            obj.value = obj.value.replace( /\s/g,'' );
        }
    }
    </script>
    
    <input type="text" onkeyup="CheckSpaces( this )">
    Aint tested it though, however it should work.
    I didn't go with this one in the end, but thank you anyway

    Quote Originally Posted by Iszak View Post
    I wouldn't rely on client-side validation as they could have JavaScript off, as for what you want to do you can easily do this.
    PHP Code:
    $string trim($string);

    if (
    preg_match('#^(\w+)$#'$string$matches) === 0)
    {
      
    // no match
    }

    else
    {
      
    // match

    You need the trim in there because the regexp won't match " word" etc. Also it will ensure the string only have one word so you're killing two birds with one stone.
    This is excellent, and worked great - thank you very, very much, +Rep :eusa_clap
    The sunlight hurts my eyes...

    ~ Love, Patrick ~


    Know your stuff about Habbo? I'm looking for high-quality article writers - PM for more!

    I am Habbox's most trusted seller of VIP/Donator - over 100 months total sold without issue.

  10. #10
    Join Date
    Aug 2005
    Location
    Standing on the rooftops...
    Posts
    1,501
    Tokens
    6
    Habbo
    ReviewDude

    Latest Awards:

    Default

    Quote Originally Posted by Iszak View Post
    I wouldn't rely on client-side validation as they could have JavaScript off, as for what you want to do you can easily do this.
    PHP Code:
    $string trim($string);

    if (
    preg_match('#^(\w+)$#'$string$matches) === 0)
    {
      
    // no match
    }

    else
    {
      
    // match

    You need the trim in there because the regexp won't match " word" etc. Also it will ensure the string only have one word so you're killing two birds with one stone.
    How do I get this to allow users to use apostrophes?
    The sunlight hurts my eyes...

    ~ Love, Patrick ~


    Know your stuff about Habbo? I'm looking for high-quality article writers - PM for more!

    I am Habbox's most trusted seller of VIP/Donator - over 100 months total sold without issue.

Page 1 of 2 12 LastLast

Posting Permissions

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