
PHP, over the years, has evolved into a vast language with many useful functions that are overlooked. Here’s 5 functions that you have probably never used to help make your next web application easier to create.
wordwrap()
wordwrap() will, as the name suggests, wrap words. After a specified amount of characters you can separate words with characters you choose. Here’s the basic format:
wordwrap($string, $char, $separator);
Example:
$string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; echo wordwrap($string, 50, "\n"); /* Outputs: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. */
money_format()
money_format() is a useful function which will let you format a number to be displayed like money. The PHP manual shows the available formatting options.
Example:
$amount = 9999.99;
setlocale(LC_MONETARY, 'en_US');
echo money_format("%n", $amount); // Outputs "USD 9,999.99"
similar_text()
similar_text() allows you to figure out how similar two strings are. Here’s the way you call it:
similar_text($string1, $string2, $refvar);
$refvar will be assigned to the percent similarity of the two strings. 100 being that they’re the same and 0 being that they’re nothing alike.
Example:
similar_text("html", "tricks", $ref);
echo $ref; // Outputs 20
similar_text("html", "xml", $ref);
echo $ref; // Outputs 57.142857142857
similar_text("html", "html", $ref);
echo $ref; // Outputs 100
highlight_string()
highlight_string() provides syntax highlighting for PHP code. It’s very easy to use:
highlight_string($code);
Example:
$code = '<?php echo $x . "hello, world!"; ?>'; highlight_string($code);
This example would output the following:
<?php
echo $x . "hello, world!";
?>
metaphone()
Last but not least, metaphone() takes a string and returns an approximate pronunciation of it.
Example:
echo metaphone('tricks'); // Outputs "TRKS"
echo metaphone('website'); // Outputs "WBST"
Are we missing any functions? Share any obscure but useful PHP functions that you know about in a comment below. We hope these helped you!

13 Comments
In your code maybe…
They are underused for a reason. Unless all you do is blogging, these functions may never see the light of day.
I don’t think so. Some of them are pretty useful.
Really just depends on what your script needs.
I actually am curious, can you defend why you label these as underused?
wordwrap() maybe. format_money(), sure… but metaphone()? similar_text()? How are these actually underused? What use-cases are these useful for?
similar_text() I could see being used with one of those security questions, where you could check if the answer is over 95% close than they’ll count it as correct (example).
highlight_string() is used in most forum softwares to provide fast PHP syntax highlighting, by the way.
I’d go as far to say all forums use highlight_string(), since it requires less work to set up (as apposed to something like GeSHi, where you’d have to upload the library and then embed it).
Not all of them, just the PHP based forum systems.
Ah, quite right. My mistake.
I like all these functions and think they should be readily available in just about every place where there is a text box! Accept on social network sites such as Facebook where the posts could become messy and clash with the uniformity of the site. Glad you posted this code here though for us who are making sites.
Your right. I can’t remember the last time I used any of those.
Learn something new every day, especially when I come to a programing language.
Thanks for the info.
My feeling is that it won’t hurt for these functions to be made more available. Even if only a few people use them, then that should make a few people happier than they would have been without them. Also, I think that you can’t really tell how many will make use of them until you give people access to them.
Visit our site and know more about
[url=http://www.dymatize-nutrition.pl/dymatize-super-amino.html]Super Amino[/url]
Braided sleeving can be made from any thermoplastic material. Polyolefin and polyvinyl chloride are the most popular choices in material to create this form of plastic sleeving.
3 Trackbacks
5 Underused PHP Functions | HTML-Tricks…
PHP, over the years, has evolved into a vast language with many useful functions that are overlooked. Here’s 5 functions that you have probably never used to help make your next web application easier to create….
[...] 5 Underused PHP Functions [...]
[...] http://html-tricks.com/php/5-underused-functions/ Like Unlike share this with your people [...]