00:00
00:00
Newgrounds Background Image Theme

LOCKdev just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: String Manipulation

3,000 Views | 5 Replies
New Topic Respond to this Topic

Php: String Manipulation 2006-08-12 13:16:54


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

BBS Signature

Response to Php: String Manipulation 2006-08-12 13:19:26


I've just gone over it quickly, but you really shouldn't use strip_tags() ;)
But mainly a good tutorial =)


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

Response to Php: String Manipulation 2006-08-12 13:22:54


At 8/12/06 01:19 PM, elbekko wrote: I've just gone over it quickly, but you really shouldn't use strip_tags() ;)
But mainly a good tutorial =)

What's wrong with strip_tags()?

I liked the tutorial a lot.

Also, for those who you are going to start to list string functions that weren't included, which I know some people are going to feel the need to do, please, spare us.

You can't include every string function in one tutorial. If you think he forgot some string functions, then write an advance string manipulation tutorial.

Very nice work.

Response to Php: String Manipulation 2006-08-12 13:38:14


At 8/12/06 01:22 PM, DFox2 wrote: What's wrong with strip_tags()?

For one, if you use the allowedtags thing, you could IE allow a b tag. But then I could just do this:
[b style="font-size: 7em;">Yay</b]
strip_tags simply allows the full tag, with all given attributes ;) And since browsers aren't too strict on things, I could easily add a onMouseOver thingy showing an infinite loop of alerts.
The second thing is, if you have text written like this, IE to emphasize(sp?) something:
"It is <very> important"
It would just show "It is important" after strip_tags has done it's job.

So the better way of allowing some HTML tags is this:
<?php
$str = htmlspecialchars($str);
$str = str_replace(array("&lt;b&gt", "&lt;/b&gt;"), array("[b>", "</b]"), $str);
?>

(yeah, I had to edit it again because of the BBS missing code tags =()


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

Response to Php: String Manipulation 2006-08-12 13:41:57


At 8/12/06 01:38 PM, elbekko wrote:
At 8/12/06 01:22 PM, DFox2 wrote: What's wrong with strip_tags()?
For one, if you use the allowedtags thing, you could IE allow a b tag. But then I could just do this:
[b style="font-size: 7em;">Yay</b]
strip_tags simply allows the full tag, with all given attributes ;) And since browsers aren't too strict on things, I could easily add a onMouseOver thingy showing an infinite loop of alerts.
The second thing is, if you have text written like this, IE to emphasize(sp?) something:
"It is <very> important"
It would just show "It is important" after strip_tags has done it's job.

That's good to know and all, if you're going to use it that way and all. But if you're going to use strip_tags for it's most powerful usage, getting rid of code, it works perfectly, and should be used.

Response to Php: String Manipulation 2006-08-12 13:43:26


At 8/12/06 01:41 PM, DFox2 wrote: That's good to know and all, if you're going to use it that way and all. But if you're going to use strip_tags for it's most powerful usage, getting rid of code, it works perfectly, and should be used.

You're still risking to lose information... but yeah, that's your choice.


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature