this is a sample script that splits a word into an array. each letter becomes an element of the array. i will show you how turn a string into an array using PHP code with example scripts [1050], Last Updated: Sat May 18, 2024
wallpaperama
Thu Jun 28, 2007
0 Comments
1533 Visits
breaking a single word into an array is simple with php.
for the longest time i wanted to find out how to do this, then i visited the official website for php which is www.php.net
my original idea was that i was having a problem when people where giving their name with repeated letter or numbers. for example, i would ask for their name on the hmtl form and they would input bogus information like this: aaaaba as their name or their last name, and abviously these were just freeloaders just trying to hack the system.
well, how can you stop this?? if i only could find a way to run a script the would check each letter or character and then compare it with the previous so lets say in my example, i ask for a user's first name and they provide: aaaaba
well, aaaaba is not a valid name so i need to break it down into an array to compare each character in the string so i wanted something like this for example on my code:
so i could run a loop checking if the $string[1] was the same as $string[0] and so on, if it were the same, it would error.
ok, that would work, but how about if a user's name is allen or vanessa. as you can see, on allen, the letter L is repeated but allen is a valid name, and on vanessa, the letter S is repeated yet vanessa is a valid name.
well, vanessa might be a good name, but vanesssa is not a valid name, so what i needed to do was to check for a character that's repeated at least three times, if the script finds that a user enters a name with three or more letters that are the same, then that means its not valid. or if a user enters numbers, then you know its fake information given then you can errror you form.
so, so lets say i have an HTML web form it looks like this:
Note: this form will not work, its just for showing - so don't click on Submit
so once a user submits the form, php can start to process of breaking down the word into individual strings into an array and this is how the PHP code would look like:
CODE:
<img src="http://webune.com/images/headers/default_logo.jpg">
<h1>Checking for repeated letters by webune.com</h1><hr>
<?
if(isset($_REQUEST['Submit']))
{
# FORM HAS BEEN SUMBITTED
}
else
{
# FORM HAS NOT BEEN SUBMITTED
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
Name: <input type="text" name="name">
<input type="submit" name="Submit" value="Submit">
</form>
<?
}
?>
<hr>
<div align="center">PHP Webhosting at <a href="http://www.webune.com">www.webune.com</a></div>
Now to see this script in action, all you have to do is copy and paste into your text editor. if you have windows, you can copy and paste this code into a blank notepad and save it as webune.php then upload to your website and open it with your browser from your website.
this step by step tutorial provided free by Webune Hosting
NOTE: you MUST have PHP on your webite for this script to work. If you don't have PHP yet on your wesbite, we recommend you visit WWW.WEBUNE.COM and sign up with one of their simple PHP web hosting plans. We always recommend Webune to our customer because they have excellent service and support.