PHP:Main
String Manipulation
----------------------
Hello everyone. This is my first PHP:Main tutorial, and I'm also currently learning PHP. So tell me how I do!
----------------------
What you should know:
Basic PHP - and by basic, I mean BASIC. If you know nothing of PHP, read this.
What will I learn?
You will learn some basic and useful string functions.
WATS A STRING?!!1
A string is a flow of characters(letters, numbers, and symbols). There are 256 possible in PHP.
An example of a string: "This is a string!"
How string functions can help you:
The problem: You want to have people enter a message in your guestbook without it getting massivly spammed.
The solution: use string functions.
Some functions
Function Name: trim(); "$string" represents the string that it is using the function on.
How it's used: trim($string);
What's this do? It clears all white space of the front and back of the string. But what's whitespace? White is exactly what it says; spaces. Say David wants to sign your guestbook on your site, but unintentionally writes his name like this:
David
See all that unessisary space he did? the trim() function gets rid of it to look like this:
David
Other forms of this are rtrim and ltrim. rtrim only clears white space at the end of the string(r=right) and ltrim does the same, but only at the beginning of the string.
----
Function Name: nl2br();
How it's used: nl2br($string);
So this basically changes newlines(/n) to html <br>. Liek, Kurt wants to write a guestbook entry:
Hey! This is a cool site.
I hope to see further updates from you.
-Kurt
But without using nl2br, it will look like this:
Hey! This is a cool site. I hope to see further updates from you. -Kurt
That doesn't look right! But with nl2br, it will look exactly how Kurt typed it; with newlines(/n).
----
Function Name: wordwrap();
How it's used: wordwrap($string, width, break)
Oh no, that asshole Frank is on your site again, and he wants to spam it up by typing useless things such as askldfjo4hueothslakhgoaw4uhytowhslasrougte
hyroigthjashgasdoghyweuioarhgalsfhalsdfjas
lkjf ect. If he keeps that up for another mile or so, he could totally screw up the look of your site, keeping guests away! How can you prevent him from temporarily destroying your site? use wordwrap!
$entry = wordwrap($entry, 50, "<br>");
This will make it so in $entry, every 50 characters it will insert a <br>! Then when you get back to your site, just delete his entry and maybe ip ban him for a little bit so it doesn't happen again.
----
Fucntion Name: strlen();
How it's used: strlen($string);
Well, since you already used wordwrap() to prevent long horizontal spam entrys, you need to prevent LONG useless entries. Now, you probably wouldn't enter more than 500+ characters in someones guestbook unless you're spammming it, right? Frank is spamming, but since his spam will <br> every 50 chars, you need to prevent it from going DOWN.
Let's block people from writing 500+ entries:
if(strlen($entry)>500){
echo "Your message was over 500 characters! Go back and try again.";
exit;
}
And maybe some people will spam with EMPTY entries, so you should check if there are more than 5 characters with:
strlen($entry)<5
----
Function Name: strip_tags();
How it's used: strip_tags($string, $allowedTags) #$allowed tags parimeter is optional
There goes Chaz again, trying to screw up your site with his HTML skillz. What if Chaz enters
[font size"1000"]LOLQWERTYUIOPASDFHJKLKALSEIRASD
GHSJKLHBX[/font]
*Note: '[' and ']' equal '<' and '>' for this example.
That will screw up your site, and get by the wordwrap() function. If we strip_tags($entry) it, then HTML or PHP tags will be removed from the message. But what if innocent little Dan wants to just use the [b]bold[/b] or [i]italic[/i] tags to emphasize a phrase, he couldn't! No problem, we can also allow tags:
$entry = strip_tags($entry, "[b][i]");
*'[' = '<'
In between the quotes are allowed tags, so now people can only use [b] and [i].
----
Function Name: str_replace(); and str_ireplace();
How it's used: str_(i)replace("replace me", "with me", $in_this_string);
Danny loves to swear, and he can't stop! Some of his offensive language might offend other users! So you should use str_ireplace! Let's say Danny writes something like this:
Hey! This site is fucking nice! Oh, hold on, I have to go take a shit, FUCK YEAH!
-Danny
Now that would probably offend some people. Let's make an array of words that might offend:
$offensiveWords = array("fuck", "shit");
*Note, you can add as many words as you want, I'm using these as an example.
So lets pull a string relplace on him.
$entry = str_ireplace($offensiveWords, "%$#@!", $entry);
This replaces all words in the $offensiveWords array with %$#@! so now his message will be:
Hey! This site is %$#@!ing nice! Oh, hold on, I have to go take a %$#@!, %$#@! YEAH!
-Danny
str_ireplace can only be used in PHP 5.0+
str_ireplace is different than str_replace. str_replace is cAsE sEnSiTiVe while str_ireplace is not, so if we used str_replace, Danny's message will be:
Hey! This site is %$#@!ing nice! Oh, hold on, I have to go take a %$#@!, FUCK YEAH!
-Danny
----
Well that's all for now! Thanks for reading my first tutorial, and I hope this was somewhat useful. :)
-Zwe
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz