View Full Version : Quick PHP Help
ReviewDude
30-04-2009, 06:22 PM
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
Protege
30-04-2009, 06:30 PM
<?php
/* Get first word */
$String = 'hello my name is james and i love you';
$First_Word = current( explode( ' ', $String ) );
echo $First_Word;
Echos
hello
<?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
ReviewDude
30-04-2009, 06:39 PM
<?php
/* Get first word */
$String = 'hello my name is james and i love you';
$First_Word = current( explode( ' ', $String ) );
echo $First_Word;
Echos
hello
<?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.
Protege
30-04-2009, 06:51 PM
You mean client side?
This then
<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.
Iszak
30-04-2009, 07:26 PM
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.
$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.
Protege
30-04-2009, 07:31 PM
Yet my server-side version also did the trick.
Iszak
30-04-2009, 07:37 PM
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.
Source
30-04-2009, 07:54 PM
but he did... that was his first reply.
ReviewDude
30-04-2009, 08:28 PM
You mean client side?
This then
<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 :D
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.
$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
ReviewDude
30-04-2009, 09:46 PM
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.
$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?
Iszak
01-05-2009, 07:59 AM
Sorry for the late reply, simply do.
preg_match("#^([A-Z']+)$#i", $string, $matches)
I've changed it from \w+ because that also includes numbers and underscores, but with this new one it'll only work with A-Z, a-z, and apostrophes.
ReviewDude
01-05-2009, 08:03 AM
Sorry for the late reply, simply do.
preg_match("#^([A-Z']+)$#i", $string, $matches)
I've changed it from \w+ because that also includes numbers and underscores, but with this new one it'll only work with A-Z, a-z, and apostrophes.
You're a legend ;)
By the way, the overall project is here: http://www.reviewdude.co.uk/tlw/
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.