PHP string functions summary
Working with strings is done very often for the web, and PHP is probably one of the best programming languages for manipulating strings
I guarante that there are a function or combination of string functions for Anything you ever need.
Often it is great idea to create own string functions by combining PHP built in functions so you get exactly what you want. Creating functions is always a good idea if you think you will do a thing more than once, or if the code gets to messy just move out some lines as a function with a good name and use it! Most programmers have files with good functions to use in all projects, and string functions are a great thing to have there.
I want to help you out and give you some insipration on what you are able to do with string functions in PHP. Most functions are very basic, but I’ll try to give you some ideas on what to use them for and how to combine them with others for great funtionallity.
trim
It’s only purpose is to remove “white spaces”. It might sound unuseful, but if you think about a use writing something in your newly created input field in a form and often accidently puts in a space in the end. You often do it when you type fast. Use the function like this: $trimmedText = trim($userText); this function removes whitespaces in the beginning and in the end of the string, and also these special characters: “NULL”, “tab”, “new line”, “verticle tab” (not use often…), “carriage return”. DON’T use trim on the input from textareas, because newlines are removed, or atleast think about it…
You can use ltrim and rtrim if you only want to trim characters from the left or right of a string.
strpos(string,whatToFind,start)
This is a function you will most guaranteed use pretty often, and need to know how to handle! strpos till help you find the position of a string in another string (if it exists). A quick description of the parameters you feed this function with: string is the string you want to search IN, whatToFind is what you want to FIND, start is optional and should be used if you want to start searching from for example letter 5.
Here’s an example to get the function better. We will search one string for two different things.
$string = “donald duck”;
$whatToFind = “donald”;
$whatToFind2 = “uck”;
$pos1 = strpos($string, $whatToFind);
$pos2 = strpos($string, $whatToFind2);
echo “First strings position: ” . $pos1;
echo “Second strings position: ” . $pos2;
The result will be 0 (zero) for first string and 8 for second string. Remember that the first letter is numbered 0 (zero).
It’s now always you the string you search for is present in the string… And of course you want to check that before you continue in your code beliving the string is present. Here is a pretty trick thing that most people mistake on. You might belive that you can check if the string is present with this code:
if($pos1) echo “I found donald in the string, yippi!”;
else echo “donald is nowhere to be found in this string”;
Belive it or not, but this example will output “donald is nowhere to be found in this string”… Why? Because the string Donald is found on the first position that is called 0 (zero) and if you test this code: if(0) it will result false. PHP has something special for this that looks like this: if($pos1 === FALSE) echo “the string is not found”;
Did you see the THREE equal signs? I didn’t miss-type, you need 3 =’s to check if the string does not exist. Now promise not to forget this, because I have some times
explode
This function is often very useful if you work with files, because they can be organized in special ways to be able to have advanced information in them. Let’s say a string i divided with semicolons this is the magic function for splitting them up in an easy way.
The function is called explode and takes two parameters. First parameter is what to “split” the string on and in our example it’s semicolon. Second parameter is the string that contains all our data.
$string = “Donald;Mickey;Minnie;Bert”;
$exploded = explode(“;”, $string);
print_r($exploded);
The print_r function just outputs the array explode returns, very useful function! The array will have four elements, one for each name. Semicolons are removed, so don’t worry about them.
If you want to write out Minnie’s name you write like this:
echo $exploded[2]; //for the third element.
That’s it for today
I think explode wrappes up our tutorial for today. There will be many more string functions to get you going in the string world
Play around a little with these functions and you will probably find lots of new ways to use them. and think about my suggestion that you should encapsulate your work in functions…

May 22nd, 2009 at 7:26 am
Only some function have been given, rest of functions will be published on ur next birth
July 30th, 2009 at 12:09 am
You didn’t talk about other function like strlen().Please talk about them even if its brief, don’t worry the public will appreciate it.
August 11th, 2009 at 7:25 am
We will try to do a new post about string functions and see if we can find others that might need some explaining. Will probably include strlen() that is very useful when finding out the length of a string.
August 13th, 2009 at 3:41 pm
[...] Aug.13, 2009 in PHP A couple of years ago we published a short article with some tips on good string functions. Didn’t think it would be that appreciated, but lots of people has emailed us and thanked us [...]
August 21st, 2009 at 2:20 am
thanks, very informative.Look forward to your rss which i’ve subscribed to
August 24th, 2009 at 11:48 am
Thanks a lot, hope you will like all new tutorials we post!