Hey there, this is my first tutorial so don't be frustrated if you see an error or something, just leave a comment and I'll edit it :P
This tutorial is about how to manipulate strings in PHP, how to filter out words, how to check if a string it too long, and stuff like that.
Checking the length of a string
strlen() is the function we'll be focusing on in the section.
First, lets take a look at the following piece of code:
<?php $string = 'I like butter'; if(strlen($string)>$max){ print 'String too long!'; } ?>
Triming
But what if the input is just spaces? Then we use trim(). trim(); removed all the spaces from the beginning and end of a string. Let's see an example:
<?php $spacedString = ' This has spaces! Oh dear.'; print trim($spacedString); ?>
<?php
$postedData = $_POST['data']; // Lets say this is ' '.
//There is about 20 spaces in there.
$trimmed = trim($postedData);
if(strlen($trimmed)<1){
print "Email Invalid\";
}
?>
Replacing words
This is good for making a censored words list with. How do you do this? With str_replace()! Lets take this piece of code:
<?php
$words[] = 'Word1';
$words[] = 'Word2'; // Array of words
function censorString(){ global $words; // Get the array
$i = 0;
while($i<count($words)){
$string = str_replace($words[$i], ' ** Bleep ** ', $string); $i++;}
return $string; }
?>
<?php
Starting the php script, obviously.
$words[] = 'Word1'; // Array of words $words[] = 'Word2';
This is an array of the \"bad\" words.
function censorString(){ global $words; // Get the array $i = 0;
This is just starting a function, getting the \"bad\" words array and then setting $i to 0. We'll need the $i to loop through the array later.
while($i<count($words)){
count() returns the number of elements in an array, so in this case it returns 2. You should know about while loops, they're very simple. But this tutorial isn't about while loops, so I'll move on :P
$string = str_replace($words[$i], 'Bleep', $string);
This is when the real stuff is done. The while loop is looping through the array, adding 1 to $i every time, so that we can use it to get each element of the array in turn. str_replace()'s is used like this: str_replace(REPLACE THIS, WITH THIS, IN THIS); So now that the code has looped through all the array, it has replaced all the bad words in a string with 'Bleep'.
$i++; } return $string; }
The $i++; adds 1 to $i to stop the loop going on forever! We then close the while loop, return the string out the function and then end the function. This function would be used like this: censorString(\"This string contains word1\"); that would return \"This string contains **Bleep** \". However, if you wanted to make this more effective, you could use str_ireplace() instead of str_replace() to make the search case insensative. (Search without caring whether its a capital or not.)
Sanitizing Input
This section teaches you ways to make HTML safe to put into databases or something :)
Strip_tags()
This is not a very good method, but its effective non-the-less. It removes all html or php tags, like <b> and <a>. Used like below: strip_tags(\"This string will be <b>stripped</b>\"); Returns \"This string will be stripped\" (Note how there is no bold).
htmlentities()
htmlentites turns all html into their html entities, so © becomes &co[ b][ /b]py; and so on. This is my favourite method, and I recommend it. Used like below: htmlentities(\"<b> Ho ho ho </b>\"); returns \"&[ b][ /b]lt;b&[ b][ /b]gt; Ho ho ho &l[ b][ /b]t;/b&[ b][ /b]gt;]
addslashes()
This is very good aswell, it adds a \"\" in front of all ', \" and existing 's in a string. Used like this:
addslashes(\"I'm very well. And you're name is?\"); this outputs: \"I'm very well. And you're name is?\"
Thanks for reading!
Thanks for reading my tutorial, I'll be making another one on this subject to tell you about more String manipulation functions ;)
Comments
Post new comment