Computers and Technology
PHP tricks: substr()
I’ve decided to start a new section on DavePCGuy that deals entirely with PHP tips & tricks. What’s great about that is that I show you some of the lesser known/used PHP functions, PHP uses in new creative ways.
First up is substr(), a simple yet rarely used function that cuts up your string. You tell it where to cut, and the function cuts. Now, you’ll be thinking, why use this?
Let me fist show you how it works.
The function has several different arguments that can be used, here is an overview of the three I use:
substr($string, stringstart, stringend);
The first argument is the string (it can be added between quotes ” ” or as a $variable). Next is where the cut should start. If you want to start from the beginning of the string put a big fat “0″ there. Next is where the cut should end. By using strlen() you can find the number of characters in the string so if you want to cut the first few letter and leave the rest, use this function to find out where the “string end” is.
Example:
$websiteurl= "http://bit.ly/GoEVa"; $stringend = $strlen($websiteurl); $endletters = substr($websiteurl, 14, $stringend); echo $endletters;
This will output, or echo out, the last few letters of the website url “GoEVa”. As you can see, this is one of the uses. I, for example, used this on a previous project to store the bit.ly shortened versions of a website URL for twitter use before I refined the script.
USES:
I have a few uses, one of them, as previously mentioned, was adding shortened url’s to a database. Next one is a timestamp. Using the TIMESTAMP datatype in MYSQL, and the default value “CURRENT_TIMESTAMP”, you will always get a value in this format: YYYY-MM-DD HH-mm-ss which is way too lengthy to use anywhere. To shorten it effectively, use the substr() function:
$columndata = "YYYY-MM-DD hh-mm-ss"; $date = substr($columndata, 0, 10); echo $date
This will echo out only the date in the same format. It will show up this way: “2010-04-15″ without the quotes. You can, of course, use the date() function if you want to but this is a really quick way.
There are other uses, for example if you ask for a twitter username and someone puts in the whole URL:
$url = "http://twitter.com/wajanusdesigns"; $strlength = strlen($url); $finalurl=substr($url, 19, $strlength); $finalurl;
How about a really strange password security? Make a minimum of, let’s say 10 characters for password. Look:
$pass = $_POST['password']; $strlength = strlen($pass); $string = substr($pass, 5, $strlength); $password = sha1($string);
And put it in your database! Who would have expected THAT?
To get around the “strlen()” function use, use negative numbers if you want the last few digits:
$string = "Hello there"; $string = substr($string, -5); echo $string;
The final $string would be “there” instead of the whole thing.
As you can see there are many different creative uses for substr() function. I’m sure you can come up with more and better ones!
| Print article | This entry was posted by Admin on April 19, 2010 at 15:16, and is filed under php tricks. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


