Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Simie

Simie Avatar

******
ProScripter

1,052


May 2006
PHP String Manipulation

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 peace of code:
<?php
$string = 'I like butter';
if(strlen($string)>$max){
print 'String too long!';
}
?>

Understand that? If you don't, what the strlen() function is doing is returning the length of the string that you input.
So,
strlen('Cheese');
would return 6 and
strlen('Cake');
would return 4. Geddit?


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);
?>

This is quite simple, all this does is removed the 6 spaces infront of the string. It would also remove the spaces at the end if there were any. Simple eh? This is very useful for checking whether a $_POST input isn't just spaces. You can do that like this:
<?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";
}
?>

Cool eh?

Replacing words

This is good for making a censored words list with. How do you do this? With str_replace()!
Lets take this peace of code:
<?php
$words[] = 'Word1'; // Array of words
$words[] = 'Word2';
function censorString(){
  global $words; // Get the array
$i = 0;
while($i<count($words)){
$string = str_replace($words[$i], ' ** Bleep ** ', $string);
$i++;
}
  return $string;
}
?>

Understand any of that? If you didn't, don't worry. I'll break it down for you:
<?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[/u]

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 &copy; and so on. This is my favourite method, and I recommend it. Used like below:
htmlentities("<b> Ho ho ho </b>"); returns "&lt;b&gt; Ho ho ho &lt;/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 ;)


Last Edit: Sept 27, 2006 9:59:45 GMT by Simie

newBookmarkLockedFalling